The Python split string function is one of the String methods. Python split string function is useful 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 uses the specified separator to return a list of words.
A Python String split function start looking for the separator from the Left-Hand side. Once it finds the separator, it split the string before the Separator and adds to the list item. It repeats the process until it reaches to end position or Max_Split position. In this section, we discuss how to write String split in Python Programming with example.
Python split String Syntax
The syntax of the Python String split function is
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, the python split string function uses Empty Space as the separator.
- Max_Split: This argument is optional. If you specify this value then, split function restricts the list of words.
Return Value
Python split function returns a List of words. For example, If we have X*Y*Z and If we use * as a separator, split function search for * from left to right. Once the split function finds *, Python returns the string before the * symbol as List Item 1 (X) so on and so forth.
If you add Max_Split argument to the above example, X*Y*Z.split(‘*’, 1), python split function search for *. Once it finds *, the split function returns the string before the * symbol as List Item 1 (X) and returns the remaining string as list item 2.
Python split String Example
The following set of examples help you understand the string split in Python Programming Language.
# Python split string Example text = 'Welcome to Tutorial Gateway' string_split = text.split() print(string_split)
OUTPUT
NOTE: If you pass the non-existing item as the String method separator, then split string function return the whole string as List Item 1.
Python split string by Comma
In this example, we are splitting the strings using the comma followed by space. Within the last statement, we removed the extra space. It means the string split function split the fruits string by comma.
# Python split string Example text = 'Hi, This is Suresh, Posting Python articles' string_split = text.split(", ") print(string_split) print() fruits = 'Apple, Orange, Banana, Kiwi, Cherry, Mango, Berry' fruits_split = fruits.split(", ") print(fruits_split) # Removed Extra Space fruits_split = fruits.split(",") print(fruits_split)
OUTPUT
Split string by Space
In this example, we are splitting the given string using the space.
text = 'Learn Python at Tutorial Gateway For Free' string_split = text.split(" ") print(string_split) print() fruits = 'Apple Orange Banana Kiwi Cherry Mango Berry' fruits_split = fruits.split(" ") print(fruits_split)
OUTPUT
Python split string by Character
Here, we are using a single character to split the string. The first statement split the text using A character. The second statement split fruits using $ symbol.
text = 'LearnAPythonAatATutorialAGatewayAForAFree' string_split = text.split("A") print(string_split) print() fruits = 'Apple$Orange$Banana$Kiwi$Cherry$Mango$Berry' fruits_split = fruits.split("$") print(fruits_split)
OUTPUT
Python split string by Multiple Characters
In this example, we used multiple characters or substring to split a given string. The first statement split using ABC substring. The second statement split fruits using @#$ symbol.
text = 'LearnABCPythonABCatABCTutorialABCGatewayABCForABCFree' string_split = text.split('ABC') print(string_split) print() fruits = '[email protected]#[email protected]#[email protected]#[email protected]#[email protected]#[email protected]#$Berry' fruits_split = fruits.split("@#$") print(fruits_split)
OUTPUT
Here we only Pass either a single argument or No argument to the split function.
# Python Split String Example Str1 = 'India, China, Japan, USA, UK, Australia, Canada' Str2 = 'Learn-Python-Programming-Tutorial-Gateway' Str3 = 'xyz*abc*ijk*lmn' Str4 = Str1.split(',') print("After Splitting String 1 = ", Str4) Str5 = Str2.split('-') print("After Splitting String 2 = ", Str5) Str6 = Str3.split('*') print("After Splitting String 3 = ", Str6) #Performing Python Split function directly Str7 = 'https://www.tutorialgateway.org/python/'.split('/') print("After Splitting String 7 = ", Str7) # Split function with No arguments Str8 = 'Learn Python Programming Tutorial Gateway' Str9 = Str8.split() print("After Splitting String 8 = ", Str9)
OUTPUT
ANALYSIS
The following statement split the Str1 string based on the separator we specified (i.e., ‘,’) and prints the output.
Str4 = Str1.split(',') print("After Splitting String 1 = ", Str4)
It split the Str2 string based on the ‘-‘ symbol and prints the output.
Str5 = Str2.split('-') print("After Splitting String 2 = ", Str5)
In the below Python statement, we used the Python String split directly. The following statement split the string based on the ‘/’ symbol and prints the output.
Str7 = 'https://www.tutorialgateway.org/python/'.split('/') print("After Splitting String 7 = ", Str7)
Next, we tried to call the String split function without using any of the arguments.
Str8 = 'Learn Python Programming Tutorial Gateway' Str9 = Str8.split() print("After Splitting String 8 = ", Str9)
From the Output, you can observe that it is splitting the string based on empty space. Because that is the default argument of string split function
Python string split Multiple Delimiters
This example shows you how to use multiple delimiters to split any string.
text = 'Learn.,Python.,at.,Tutorial.,Gateway.,For.,Free' string_split = text.split('.,') print(string_split) print() fruits = 'Apple,,"Orange,,"Banana,,"Kiwi,,"Cherry,,"[email protected]#$Berry' fruits_split = fruits.split(',,"') print(fruits_split)
OUTPUT
Python string split Multiple Delimiters Example 2
It is another example of a Python split string using multiple delimiters. However, this time we are using the For Loop to iterate each list item returned by the split function, and prints them.
text = 'Learn.,Python.,at.,Tutorial.,Gateway.,For.,Free' string_split = text.split('.,') print(string_split) for txt in string_split: print(txt) print() fruits = 'Apple,,"Orange,,"Banana,,"Kiwi,,"Cherry,,"Mango,,"Berry' fruits_split = fruits.split(',,"') print(fruits_split) for ft in fruits_split: print(ft)
OUTPUT
split String Example 2
The following set of examples help you understand the advanced split options in Python Programming Language. Here, we only Pass either two arguments or No argument to the String split function.
# Split function with No arguments Str1 = 'Learn Python Programming Tutorial Gateway' Str2 = Str1.split() print("After Splitting String 1 = ", Str2) # Split function with Two arguments Str3 = 'India, China, Japan, USA, UK, Australia, Canada' Str4 = Str3.split(',', 1) print("After Splitting String 1 = ", Str4) Str5 = Str3.split(',', 3) print("After Splitting String 2 = ", Str5) Str6 = Str3.split(',', 5) print("After Splitting String 3 = ", Str6)
OUTPUT
ANALYSIS
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 restricts the split function to split one word only.
Str4 = Str3.split(',', 1) print("After Splitting String 1 = ", Str4)
The following statement split the Str1 string based on the separator we specified (i.e., ‘,’) and prints the output. Here, the second argument restricts 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 two times.
text = 'Hi This is Suresh Posting Python articles' string_split = text.split(" ", 5) print(string_split) print() fruits = 'Apple, Orange, Banana, Kiwi, Cherry, Mango, Berry' fruits_split = fruits.split(", ", 2) print(fruits_split)
OUTPUT
Split Function on Empty strings
The following set of examples 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 a comma in this Python split function.
text = 'Tutorial,Gateway' a, b = text.split(',') print(a) print(b) print() fruits = 'Apple,Orange,Banana,Kiwi,Cherry,Mango,Berry' a,b,c,d,e,f,g = fruits.split(',') print(a) print(b) print(c) print(d) print(e) print(f) print(g)
OUTPUT
split string into Chars
You can use the above-specified technique to assign and return chars from a given string. Let me show you the same in this split string into chars
text = 'Python' print(text) print() a, b, c, d, e, f = text print(a) print(b) print(c) print(d) print(e) print(f)
OUTPUT
Here, we are splitting the given time and assigning those values to hours, minutes, and seconds.
time = '23:50:59' print(time) hour,minute,second = time.split(':') print('Hours = ', hour) print('Minutes = ', minute) print('Seconds = ', second)
OUTPUT
Python split string into list of Characters
You can use the list function to convert the given string into a list of characters. Here, you don’t have to use the split function.
text = 'Python' print(text) print(list(text)) print() st = 'Tutorial Gateway' print(st) print(list(st))
OUTPUT
Here, we defined a new function to split string. This function uses for loop to iterate each character and prints.
def split_into_list(string): return [ch for ch in string] text = 'Python' print(text) print(split_into_list(text)) print() st = 'Tutorial Gateway' print(st) print(split_into_list(st))
OUTPUT