Python String split(): List, By Character, Delimiter
โก Smart Summary
Python string split() is a built-in method that breaks a string into a list of substrings using a separator. It returns the pieces as list elements and accepts an optional maxsplit limit.

What is Split in Python?
The split() function helps in string manipulation in Python. It allows you to split up a string into different substrings. It returns a list of words present in a line or a string that are separated by a delimiter string.
Why use the split() function in Python?
Here are the reasons for using the Python split() function:
- A Python program may require the manipulation of strings.
- When you want to break large-length strings or a line into many substrings or smaller strings.
- The function works even if there is no separator or there is the presence of whitespaces. It provides some output, or it breaks the string into splits.
- The split() function shares substrings in the form of elements of the string.
- The function scans the line or string for the presence of a default separator that is passed as a parameter.
- The split() function helps in effective string management in Python code.
Syntax of the split() function in Python
The split() function in Python provides a list of words in each line or a string. Such strings are always separated by a delimiter string. It provides one or more substrings from the main strings. The syntax of split() is composed of a function with two parameters termed separator and max with a return value.
The syntax of the split() function is provided below:
Syntax:
string. split (separator, max)
The following parameters of the function are described as follows:
- Separator: A separator is defined to identify the delimiter. With the use of a separator, the split() function splits the main string into multiple substrings.
- Maxsplit parameter: This is a number that is passed into the function to split the string into the maximum number of occurrences.
- Return: The split() function returns the list of strings after breaking or splitting the main string.
How to split a string involving a delimiter?
A delimiter can be defined as a string that occurs in the form of a space, comma, semicolon, colon, and any other character. The function outputs the starting and ending of the character string, word, or data item.
Let us consider the below Python example for the usage of the split() function with the involvement of a delimiter.
Python code:
Python code:
text = "Hello World Guru99"
# splits at space
print(text.split())
text = "Hello,World, Guru99"
# splits at ','
print(text.split(", "))
text = "Hello,World:, Guru99"
# Splits at ':'
print(text.split(":"))
Output:
['Hello', 'World', 'Guru99'] ['Hello,World', 'Guru99'] ['Hello,World', ', Guru99']
In the above code, the string is separated using delimiters colon, space, and comma.
How to split a string on the occurrence of any character present in the string?
The split() function in Python can be used in numerous ways. One such scenario could be the usage of the split() function on the occurrence of a character as a delimiter. A split() function can be used to identify a character that could be used in place of, or as a substitute for, a delimiter present in the main string to split a string.
Let us consider the below example for the usage of the split() function with the involvement of a character being used as a delimiter in the given string.
Python code:
text='Hello World Guru99'
#splits with character occurrence
print(text.split("l"))
Output:
['He', '', 'o Wor', 'd Guru99']
In the above code, the string is separated on the occurrence of a character used as a delimiter. Here the defined character is ‘l’.
How to split the string into a list?
The split() function can also be used to split a string to be printed in the form of a list. Here, you can use a loop to print a string as different substrings in the form of a list.
The following example shows the usage of the split() function.
Python code:
text='Hello World Guru99'
#splits with string in the form of list
list_1 = text.split()
for st in list_1:
print(st)
Output:
Hello World Guru99
In the above code, the string is separated and then listed one after another with the help of a loop.
How to split a string by application of the max split operator?
The split() function also provides an option of splitting the string into a maximum number of times. If the value is not initialized, then it gives -1 as the default value.
Let us consider the below example for the usage of the split() function.
Python code:
text='Hello World Guru99'
split_1 = text.split(',',2)
print(split_1)
text='Hello World Guru99'
split_1 = text.split(',',4)
print(split_1)
Output:
['Hello World Guru99'] ['Hello World Guru99']
In the above Python code, the string is separated on the basis of the maximum number of instances.
How to use the split() function using a substring in Python?
A substring can be defined as the string that is present within the main string. The split() function can be used to split the main string based on the substring present in that string.
Let us consider the below example for the usage of the split() function.
Python code:
text='Hello World Guru 99'
split_1 = text.split('Guru',2)
print(split_1[1])
text='Hello World Guru99'
split_1 = text.split('Guru',2)
print(split_1[0])
Output:
99 Hello World
In the above code, the main string is separated on the basis of the substring ‘Guru’ present in the given string, so the parts before and after ‘Guru’ are returned.
Importance of the split() function in Python
The split() function can be a very handy Python function when working on a text mining project. A split() function can be used in several customizable forms. It could split strings on the initialization of the delimiter. Even if the delimiter is not present, the split() function can still be used to divide a string based on the white spaces present in the string.
The split() function also splits a string in the presence of multiple delimiters. This shows how dynamic and critical the split() function in Python is.
