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.

Example: Split a string on whitespace

S = 'The World is Beautiful'
x = S.split()
print(x)    # ['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.

Example: Split a string containing multiple whitespace

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

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

Split on a Delimiter

You can split a string by specifying a delimiter.

Example: Split on comma

S = 'red,green,blue'
x = S.split(',')
print(x)	# ['red', 'green', 'blue']

Example: Split on new line

S = 'First Line\nSecond Line'
x = S.split('\n')
print(x)	# ['First Line', 'Second Line']

A delimiter can contain multiple characters.

Example: Delimiter with multiple characters

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

Limit Splits With Maxsplit

When you specify maxsplit, however, only the given number of splits will be made.

The list will have the specified number of elements plus one.

Example: Limit splits in split() method

S = 'The World is Beautiful'
x = S.split(None,1)
print(x)	# ['The', 'World is Beautiful']
S = 'The World is Beautiful'
x = S.split(None,2)
print(x)	# ['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).

Example: split() method with default maxsplit

S = 'The World is Beautiful'
x = S.split(None,-1)
print(x)	# ['The', 'World', 'is', 'Beautiful']
S = 'The World is Beautiful'
x = S.split()
print(x)    # ['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.

Example: Split with comma ( , ) semicolon ( ; ) and colon ( : )

S = 'red,green;blue:yellow'
import re
x = re.split('[,;:]',S)
print(x)	# ['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.

Example:

# split()
S = 'The World is Beautiful'
x = S.split(None,1)
print(x)	# ['The', 'World is Beautiful']
# rsplit()
S = 'The World is Beautiful'
x = S.rsplit(None,1)
print(x)	# ['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.

Example:

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