Python Split String

The Python split string function is one of the String Method. It is used to split the given string and return a list of words. This Python split string function accepts two arguments (optional). If you specify the separator to split then it will use the specified separator to return a list of words.

A Python String Split function will start looking for the separator from Left Hand side. Once it finds the separator, it will split the string before the Separator and add to list item. It will repeat the process until it reaches to end position or Max_Split position.

In this article we will show you, How to write String Split in Python Programming with example.

Python Split String Syntax

The basic syntax of the Python String Split function is as shown below:

String_Value.split(Separator, Max_Split)

String_Value: Please select the valid String variable or you can use the String directly.

Separator: This argument is optional and if you forget this argument, python split string function will use Empty Space as the separator.

Max_Split: This argument is optional. If you specify this value then, split function will restrict the list of words.

Return Value

Python Split function returns List of words. For example, If we have X*Y*Z and If we use * as separator, Split function will search for * from left to right. Once it find * it will return the string before the * symbol as List Item 1 (X) so on and so forth.

If you add Max_Split argument to above example, X*Y*Z.split(‘*’, 1), python split function will search for *. Once it find * it will return the string before the * symbol as List Item 1 (X) and return the remaining string as list item 2.

NOTE: If you pass the non existing item as the separator then split string function will return the whole string as List Item 1.

Python split String Example

The following set of examples will help you understand the string Split in Python Programming Language.

Python string split Multiple Delimiters Example 2

This is an another example to split string using multiple delimiters. However, this time we are using the For Loop to iterate each and every list item returned by Python split function, and prints them.

Split String Example 2

The following set of examples will help you understand the advanced Split options in Python Programming Language. Here we will only Pass either two argument or No argument to the Python String split function

First we declared a String variable Str3 and assigned corresponding value using following statement

Str3 = 'India, China, Japan, USA, UK, Australia, Canada'

The following split string statement was splitting the Str1 string based on the separator we specified (i.e, ‘,’) and prints the output. Here the second argument will restrict the split function to split one word only.

Str4 = Str3.split(',', 1)
print("After Splitting String 1 = ", Str4)

Following statement will split the Str1 string based on the separator we specified (i.e, ‘,’) and prints the output. Here the second argument will restrict the split function to split three words only.

Str5 = Str3.split(',', 3)
print("After Splitting String 2 = ", Str5)

Python String split count example 2

This time, we are splitting text using space for 5 times. Next, we used comma and space to split fruits and it splits for two times.

Split Function on Empty strings

The following set of examples will help you understand the functionality of a Python Split function while working with empty data.

String Split Example 3

Until now, we are printing the list returned by the split function. Here, we are assigning the splitter values to variables. To do so, you have to provide the variables separated by comma in this Python split function.