Python String split() Function
The String split()
method splits the string on a specified delimiter and returns the list of substrings.
If a delimiter is not specified, the string is split on whitespace.
By default, the split()
method will make all possible splits (no limit on the number of splits). However, if you specify maxsplit
, then only the given number of splits will be made, starting from the beginning of the string.
Syntax
my_string.split(delimiter, maxsplit)
split() Parameters
Python String split()
function parameters:
Parameter | Condition | Description |
---|---|---|
delimiter | Optional | Any character to split the sting with. Default value is whitespace. |
maxsplit | Optional | A number specifying how many splits to make. Default value is -1 (i.e. no limit on splits) |
split() Return Value
Python String split()
function returns a list of substrings obtained by splitting the original string.
Examples
Example 1: Split a String on Whitespace with split()
When delimiter
parameter is not specified, the string is split on whitespace.
my_str = 'This is Tutorial Reference'
result = my_str.split()
print(result) # Output: ['This', 'is', 'Tutorial', 'Reference']
output
['This', 'is', 'Tutorial', 'Reference']
Note that the split()
method automatically combines consecutive whitespace into single delimiter and splits the string
my_str = 'This is Tutorial Reference'
result = my_str.split()
print(result) # Output: ['This', 'is', 'Tutorial', 'Reference']
output
['This', 'is', 'Tutorial', 'Reference']
Moreover, newline \n
, tab \t
and carriage return \r
are also considered as whitespace characters.
my_str = 'This\n\ris\tTutorial \n Reference'
result = my_str.split()
print(result) # Output: ['This', 'is', 'Tutorial', 'Reference']
output
['This', 'is', 'Tutorial', 'Reference']
Example 2: Split a String on Custom Delimiter with split()
You can split a string by specifying a custom delimiter
.
my_str = 'Tom, Anna, David, Ryan'
result = my_str.split(',')
print(result) # Output: ['Tom', ' Anna', ' David', ' Ryan']
output
['Tom', ' Anna', ' David', ' Ryan']
my_str = 'First Line\nSecond Line\nThird Line'
result = my_str.split('\n')
print(result) # Output: ['First Line', 'Second Line', 'Third Line']
output
['First Line', 'Second Line', 'Third Line']
Note that a delimiter
can contain multiple characters.
my_str = 'This is Tutorial Reference'
result = my_str.split(' is ')
print(result) # Output: ['This', 'Tutorial Reference']
output
['This', 'Tutorial Reference']
Example 3: Split a String for a Certain Number of Times with split()
You can specify maxsplit
parameter: it means that only the given number of splits will be made. The resulting list will have the specified number of elements plus one.
For example, split using whitespace as delimiter
(None in order to use default value) and maxsplit
to 1:
my_str = 'This is Tutorial Reference'
result = my_str.split(None, 1)
print(result) # Output: ['This', 'is Tutorial Reference']
output
['This', 'is Tutorial Reference']
But, we can change the maxsplit
to increase the number of splits:
my_str = 'This is Tutorial Reference'
result = my_str.split(None, 2)
print(result) # Output: ['This', 'is', 'Tutorial Reference']
output
['This', 'is', 'Tutorial Reference']
Note that if maxsplit
is not specified or it is -1
, then split()
method will make all possible splits (i.e. no limit on the number of splits).
my_str = 'This is Tutorial Reference'
result1 = my_str.split(None, -1) # maxsplit = -1
result2 = my_str.split(None) # maxsplit not specified
print(result1) # Output: ['This', 'is', 'Tutorial Reference']
print(result1) # Output: ['This', 'is', 'Tutorial Reference']
output
['This', 'is', 'Tutorial', 'Reference']
['This', 'is', 'Tutorial', 'Reference']
Example 4: Split a String with multiple Delimiters with split()
The split()
method does not allow for multiple delimiters. Instead, you can use regular expression with re.split()
method.
For example, let's use regular expression to split with comma ,
, semicolon ;
and colon :
:
import re # import regular expression package
my_str = 'Tom,Ryan;Anna:David'
result = re.split('[,;:]',my_str)
print(result) # Output: ['Tom', 'Ryan', 'Anna', 'David']
output
['Tom', 'Ryan', 'Anna', 'David']
split() method vs rsplit() method
- If
maxsplit
is specified, thesplit()
method counts splits from the beginning of the string, whilersplit()
method counts them from end of the string. - If
maxsplit
is not specified, they both behave exactly the same.
# split()
my_str = 'This is Tutorial Reference'
result = my_str.split(None, 1)
print(result) # Output: ['This', 'is Tutorial Reference']
# rsplit()
my_str = 'This is Tutorial Reference'
result = my_str.rsplit(None, 1)
print(result) # Output: ['This is Tutorial', 'Reference']
output
['This', 'is Tutorial Reference']
['This is Tutorial', 'Reference']
Unpacking, Indexing and Slicing the result of split()
The split()
method returns a list of strings. So, you can perform any operation on lists, like multiple assignment (unpacking), indexing, slicing, etc.
Some examples:
# multiple assignment
my_str = 'Tom,David,Anna'
x,y,z = my_str.split(',')
print(x) # Output: Tom
print(y) # Output: David
print(z) # Output: Anna
# indexing
my_str = 'Tom,David,Anna,Ryan'
result = my_str.split(',')[2]
print(result) # Output: Anna
# slicing
my_str = 'Tom,David,Anna,Ryan'
result = my_str.split(',')[1:3]
print(result) # Output: ['David', 'Anna']
output
Tom
David
Anna
Anna
['David', 'Anna']