Python String split() Method

Splits a string into a list of substrings

Usage

The split() method splits the string on a specified delimiter and returns the list of substrings. When delimiter is not specified, the string is split on whitespace.

By default, split() will make all possible splits (there is no limit on the number of splits). When you specify maxsplit, however, only the given number of splits will be made.

Syntax

string.split(delimiter,maxsplit)

Python string split() method parameters
ParameterConditionDescription
delimiterOptionalAny character to split the sting with.
Default is whitespace.
maxsplitOptionalA number specifying how many splits to make.
Default value is -1 (no limit on splits)

Split on Whitespace

When delimiter is not specified, the string is split on whitespace.

S = 'The World is Beautiful'
x = S.split()
print(x)
# Prints ['The', 'World', 'is', 'Beautiful']

Another feature of the bare call to split() is that it automatically combines consecutive whitespace into single delimiter, and splits the string.

S = '  The    World is  Beautiful'
x = S.split()
print(x)
# Prints ['The', 'World', 'is', 'Beautiful']

Newline '\n', tab '\t' and carriage return '\r' are also considered as whitespace characters.

S = 'The\n\rWorld\tis Beautiful'
x = S.split()
print(x)
# Prints ['The', 'World', 'is', 'Beautiful']

Split on a Delimiter

You can split a string by specifying a delimiter.

# Split on comma
S = 'red,green,blue'
x = S.split(',')
print(x)
# Prints ['red', 'green', 'blue']
# Split on new line
S = 'First Line\nSecond Line'
x = S.split('\n')
print(x)
# Prints ['First Line', 'Second Line']

A delimiter can contain multiple characters.

S = 'the beginning is the end is the beginning'
x = S.split(' is ')
print(x)
# Prints ['the beginning', 'the end', 'the beginning']

Limit Splits With Maxsplit

When you specify maxsplit, only the given number of splits will be made. The resulting list will have the specified number of elements plus one.

S = 'The World is Beautiful'
x = S.split(None,1)
print(x)
# Prints ['The', 'World is Beautiful']
S = 'The World is Beautiful'
x = S.split(None,2)
print(x)
# Prints ['The', 'World', 'is Beautiful']

If maxsplit is not specified or -1, split() will make all possible splits (there is no limit on the number of splits).

S = 'The World is Beautiful'
x = S.split(None,-1)
print(x)
# Prints ['The', 'World', 'is', 'Beautiful']
S = 'The World is Beautiful'
x = S.split()
print(x)
# Prints ['The', 'World', 'is', 'Beautiful']

Split on Multiple Delimiters

The split() method does not allow for multiple delimiters. You can use the re.split() method (based on regular expression) instead.

# Split with comma ( , ) semicolon ( ; ) and colon ( : )
S = 'red,green;blue:yellow'
import re
x = re.split('[,;:]',S)
print(x)
# Prints ['red', 'green', 'blue', 'yellow']

split() vs rsplit()

If maxsplit is specified, split() counts splits from the left end, whereas rsplit() counts them from right. Otherwise, they both behave exactly the same.

# split()
S = 'The World is Beautiful'
x = S.split(None,1)
print(x)
# Prints ['The', 'World is Beautiful']
# rsplit()
S = 'The World is Beautiful'
x = S.rsplit(None,1)
print(x)
# Prints ['The World is', 'Beautiful']

Unpacking, Indexing and Slicing

As split() method returns a list of substrings, you can perform any operation that a list supports. Like multiple assignment(unpacking), indexing, slicing etc.

# multiple assignment
S = 'red,green,blue'
x,y,z = S.split(',')
print(x)
# Prints red
print(y)
# Prints green
print(z)
# Prints blue
# indexing
S = 'red,green,blue,yellow'
x = S.split(',')[2]
print(x)
# Prints blue
# slicing
S = 'red,green,blue,yellow'
x = S.split(',')[1:3]
print(x)
# Prints ['green', 'blue']