In this Python tutorial, we will learn the syntax of split() function, and how to use this function to split a string based on the delimiter string and maximum number of splits that can be made.
Python – Split String
To split a String in Python with a delimiter, use split() function.
split() function splits the string into substrings and returns them as an array.
Syntax of split()
The syntax of split() function is
string.split(separator, max)
where
Parameter | Optional/ Mandatory | Description |
---|---|---|
separator | Optional | A string. Default value is white space. This is the delimiter by which the string is split into parts. |
max | Optional | An integer. This is the maximum number of splits the string can be split into. Default value does not limit the splits. |
Return Value
A list.
Examples
The following examples cover some of the use cases of splitting a string.
1. Split string with white space as separator
In this example, we take a string and split it into multiple strings with space as separator.
The default value of separator parameter is a white space, hence we shall not pass any parameters to split() function.
Example.py
#a string
str = "Welcome to Python Tutorial by TutorialKart"
#split with no separator passed
parts = str.split()
print(parts)
Output
['Welcome', 'to', 'Python', 'Tutorial', 'by', 'TutorialKart']
We can also pass white space as separator.
Example.py
#a string
str = "Welcome to Python Tutorial by TutorialKart"
#split with single space as separator
parts = str.split(' ')
print(parts)
Output
['Welcome', 'to', 'Python', 'Tutorial', 'by', 'TutorialKart']
2. Split string with comma as separator
In this example, we take a string and split it into multiple strings with comma as separator.
This example demonstrates how to extract values as an array when the input is a comma separated string.
Example.py
#a string
str = "0.124,0.547,4.125,1.2,10.63"
#split with comma as separator
values = str.split(',')
print(values)
Output
['0.124', '0.547', '4.125', '1.2', '10.63']
3. Split string with another string as separator
In this example, we take a string and split it into multiple strings with another string as separator.
Example.py
#a string
str = "hello---world---welcome---to---tutorialkart"
#split with another string as separator
values = str.split('---')
print(values)
Output
['hello', 'world', 'welcome', 'to', 'tutorialkart']
4. Split string with separator at the end of string
If you have the separator at the very start of this string or at the very end of this string, the output contains an empty element at the start or end of the array respectively.
Example.py
#a string
str = "---hello---world---welcome---to---tutorialkart---"
#split with another string as separator
values = str.split('---')
print(values)
Output
['', 'hello', 'world', 'welcome', 'to', 'tutorialkart', '']
5. Split string with maximum number of splits specified
If you specify the max
parameter, the split() method consider only the first max
number of separators in the given string.
Example.py
#a string
str = "hello---world---welcome---to---tutorialkart"
#split with another string as separator
values = str.split('---', 2)
print(values)
Output
['hello', 'world', 'welcome---to---tutorialkart']
Conclusion
In this Python Tutorial, we learned to split a string in Python using separator and also with the exception of maximum number of splits.