Chapters
Python114 chapters

ReferenceString methods

str.split()

Breaks a string into a list of pieces.

Signature

text.split(sep=None, maxsplit=-1)

Parameters

NameMeans
sepwhat to split on; any whitespace when omitted
maxsplithow many splits to make at most

Returns

A list of strings.

Example

Python
print("a,b,c".split(","))
print("  a   b ".split())
print("  a   b ".split(" "))
print("a,b,c".split(",", 1))

Output

['a', 'b', 'c']
['a', 'b']
['', '', 'a', '', '', 'b', '']
['a', 'b,c']

See also