Python split String

This Python function splits the given string and returns a list of words, and it accepts two arguments (optional). If you specify the separator, then the 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 separates the text before the Separator and adds it to the list item. The function repeats the process until it reaches to end position or Max position.

Python split String Syntax

The syntax of the String split function is

StrValue.split(Separator, Max)
  • Separator (Optional arg): If you forget this argument, it uses Empty Space as the separator.
  • Max: This argument is optional. If you specify this value, then it restricts the list of words.

The split function returns a List of words. For example, If we have X*Y*Z and If we use * as a separator, it searches for * from left to right. Once it finds *, it returns the string before the * symbol as List Item 1 (X) and so on.

If you add the max argument to the above example, X*Y*Z(‘*’, 1), it searches for *. Once it finds *, it returns the sentence before the * symbol as List Item 1 (X) and returns the remaining as list item 2.

Python String split Example

The following set of examples helps you understand this split string function.

text = 'Welcome to Tutorial Gateway'
 
sp = text.split()
 
print(sp)
['Welcome', 'to', 'Tutorial', 'Gateway']

NOTE: If you pass the non-existing item as the method separator, then it returns the whole sentence as List Item 1.

Python string split by Comma

In this example, we are separating the sentences using the comma followed by whitespace. Within the last statement, we removed the extra whitespace. It means this separates the fruits string by a comma.

Python String Split 2

split string by Space and Character

We are breaking the given text using whitespace.

text = 'Learn Pyth at Tutorial Gateway For Free'
 
st1 = text.split(" ")
print(st1)
['Learn', 'Pyth', 'at', 'Tutorial', 'Gateway', 'For', 'Free']

Here, we are using a single character to break the sentence. The first statement separates the text using the A character. The second statement uses the $ symbol for fruits.

fruits = 'Apple$Orange$Banana$Kiwi$Cherry$Mango$Berry'
fruits2 = fruits.split("$")
print(fruits2)
['Apple', 'Orange', 'Banana', 'Kiwi', 'Cherry', 'Mango', 'Berry']

Python split string by Multiple Characters

Here, we used multiple characters or substrings to break a given text. The first statement breaks using the ABC substring.

text = 'LearnABCPythABCatABCTutorialABCGatewayABCForABCFree'
 
st3 = text.split('ABC')
print(st3)
['Learn', 'Pyth', 'at', 'Tutorial', 'Gateway', 'For', 'Free']

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

In this example, we only Pass either a single argument or No argument to the function. The following Str4 statement breaks the Str1 based on the separator we specified (i.e., ‘,’) and prints the output.

In the below Python Str7 statement, we used this method directly. The following statement separates the text based on the ‘/’ symbol and prints the output.

In the last line, we tried to call the method without using any of the arguments.

Example 4

From the Output, you can observe that it breaks the text based on empty white space. Because that is the default argument of this method.

More than one Delimiter

This Python example shows you how to use string split function with multiple delimiters to separate any sentence.

text = 'Learn.,Py.,at.,Tutorial.,Gateway.,For.,Free'
 
st4 = text.split('.,')
print(st4)
 
fruits = 'Apple,,"Orange,,"Banana,,"Kiwi,,"Cherry,,"Mango@#$Berry'
fruits4 = fruits.split(',,"')
print(fruits4)
['Learn', 'Py', 'at', 'Tutorial', 'Gateway', 'For', 'Free']

['Apple', 'Orange', 'Banana', 'Kiwi', 'Cherry', 'Mango@#$Berry']

It is another example of using multiple delimiters. However, this time we are using the For Loop to iterate each list item returned by the method and print them as an output.

text = 'Learn.,Py.,at.,Tutorial.,Gateway.,For.,Free'
 
st5 = text.split('.,')
print(st5)
 
for txt in st5:
    print(txt)
 

fruits = 'Apple,,"Orange,,"Banana,,"Kiwi,,"Cherry,,"Mango,,"Berry'
fruits5 = fruits.split(',,"')
print(fruits5)
 
for ft in fruits5:
    print(ft)
['Learn', 'Py', 'at', 'Tutorial', 'Gateway', 'For', 'Free']
Learn
Py
at
Tutorial
Gateway
For
Free

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

Advanced Python String split Examples

The following set of examples helps you understand the advanced splitting options. Here, we only pass two or no arguments to the function.

For Str4, it was separating the Str1 based on the separator we specified (i.e., ‘,’) and prints the output. Here, the second argument restricts the function to break one word only.

For Str5, it separates the Str1 based on the separator we specified (i.e., ‘,’) and prints the output. Here, the second argument restricts the result to three words only.

Python Split String Example 5

This time, we are breaking text using space 5 times. Next, we used a comma and space for fruits, which breaks twice.

txt = 'Hi This is Suresh Posting Py articles'
 
str6 = txt.split(" ", 5)
print(st6)
print()
 
fruits = 'Apple, Orange, Banana, Kiwi, Cherry, Mango, Berry'
fruits6 = fruits.split(", ", 2)
print(fruits6)
['Hi', 'This', 'is', 'Suresh', 'Posting', 'Py articles']

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

Empty Text

Understand the functionality of this Python split string function while working with empty data. For instance, if we use this with the following whitespace, the output will be

  • ‘ ‘ = []
  • ‘ ‘ = []
  • ‘ ‘ = []
  • ‘ ‘.split(‘ ‘, 1) returns [”, ‘ ‘]

Until now, we are printing the list returned by the function. Here, we are assigning the separated values to variables. To do so, you must provide the variables separated by a comma.

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)
Apple
Orange
Banana
Kiwi
Cherry
Mango
Berry

Into Characters

You can use the above-specified technique to assign and return chars from a given sentence. Let me show you the same in this example.

s1 = 'ELearn'
print(s1)
print()
 
a, b, c, d, e, f = s1
 
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
ELearn

E
L
e
a
r
n

Here, we are breaking 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

split string into list of Characters

You can use the list method to convert the given text into a list of characters. Here, you don’t have to use this function.

text = 'Happy'
print(text)
print(list(text))
print()
 
st = 'Hello World'
print(st)
print(list(st))
Happy
['H', 'a', 'p', 'p', 'y']

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

Here, we defined a new function, and it uses for loop to iterate each character and print.

def strToLi(words):
    return [ch for ch in words]
 
sa = 'Pyth'
print(sa)
print(strToLi(sa))
print()
 
st = 'Tutorial Gateway'
print(st)
print(strToLi(st))
Pyth
['P', 'y', 't', 'h']

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