Skip to content
String Methods

split()

Split text into a list of pieces.

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

str.split() splits the string into a list of substrings using sep as the delimiter.

If sep is omitted or None, any consecutive whitespace is treated as a delimiter and leading/trailing whitespace is discarded.

Parameters

NameDescription
sepThe separator to split on; if omitted, whitespace is used.
maxsplitMaximum number of splits; -1 means no limit.

Returns

A list of pieces from the string.

Try it

Run it and change it
text = "red,green,blue"
print(text.split(","))

Worth knowing

  • Whitespace splitting: 'a b c'.split() splits on arbitrary runs of spaces, tabs, and newlines into ['a', 'b', 'c'].

Related entries