Python split String

The 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 the Python split string function 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. The Python split string function repeats the process until it reaches to end position or Max_Split position.

Python split String Syntax

The syntax of the Python String split function is

String_Value.split(Separator, Max_Split)
  • String_Value: A valid String variable, or you can use the String directly.
  • Separator (Optional arg): 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.

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)
['Welcome', 'to', 'Tutorial', 'Gateway']

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)
Python Split String 2

Split string by Space

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)

String split by spaces output

['Learn', 'Python', 'at', 'Tutorial', 'Gateway', 'For', 'Free']

['Apple', 'Orange', 'Banana', 'Kiwi', 'Cherry', 'Mango', 'Berry']

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)

split string by character output

['Learn', 'Python', 'at', 'Tutorial', 'Gateway', 'For', 'Free']

['Apple', 'Orange', 'Banana', 'Kiwi', 'Cherry', 'Mango', 'Berry']

Python split string by Multiple Characters

Here, 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)

split string by multiple characters output

['Learn', 'Python', 'at', 'Tutorial', 'Gateway', 'For', 'Free']

['Apple', 'Orange', 'Banana', 'Kiwi', 'Cherry', 'Mango', 'Berry']

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)

String split function with and without arguments output

After Splitting String 1 =  ['India', ' China', ' Japan', ' USA', ' UK', ' Australia', ' Canada']
After Splitting String 2 =  ['Learn', 'Python', 'Programming', 'Tutorial', 'Gateway']
After Splitting String 3 =  ['xyz', 'abc', 'ijk', 'lmn']
After Splitting String 7 =  ['https:', '', 'www.tutorialgateway.org', 'python', '']
After Splitting String 8 =  ['Learn', 'Python', 'Programming', 'Tutorial', 'Gateway']

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

String split Multiple Delimiters

This example shows you how to use Python split string function with 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)

String split by multiple delimiters output

['Learn', 'Python', 'at', 'Tutorial', 'Gateway', 'For', 'Free']

['Apple', 'Orange', 'Banana', 'Kiwi', 'Cherry', '[email protected]#$Berry']

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)

String split function with Multiple Delimiters output

['Learn', 'Python', 'at', 'Tutorial', 'Gateway', 'For', 'Free']
Learn
Python
at
Tutorial
Gateway
For
Free

['Apple', 'Orange', 'Banana', 'Kiwi', 'Cherry', 'Mango', 'Berry']
Apple
Orange
Banana
Kiwi
Cherry
Mango
Berry

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)
After Splitting String 1 =  ['Learn', 'Python', 'Programming', 'Tutorial', 'Gateway']
After Splitting String 1 =  ['India', ' China, Japan, USA, UK, Australia, Canada']
After Splitting String 2 =  ['India', ' China', ' Japan', ' USA, UK, Australia, Canada']
After Splitting String 3 =  ['India', ' China', ' Japan', ' USA', ' UK', ' Australia, Canada']

This 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)

It 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)

String split count output

['Hi', 'This', 'is', 'Suresh', 'Posting', 'Python articles']

['Apple', 'Orange', 'Banana, Kiwi, Cherry, Mango, Berry']

Split Function on Empty strings

Understand the functionality of a Python Split function while working with empty data.

Python Split String Example 3

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)
Tutorial
Gateway

Apple
Orange
Banana
Kiwi
Cherry
Mango
Berry

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)

split string into characters output

Python

P
y
t
h
o
n

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)
23:50:59
Hours   =  23
Minutes =  50
Seconds =  59

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 = 'Happy'
print(text)
print(list(text))
print()
 
st = 'Hello World'
print(st)
print(list(st))

split string into list of characters output

Happy
['H', 'a', 'p', 'p', 'y']

Hello World
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

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))
Python
['P', 'y', 't', 'h', 'o', 'n']

Tutorial Gateway
['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', ' ', 'G', 'a', 't', 'e', 'w', 'a', 'y']