Python 3 reference · String Methods

Python String split() Method

Split the string into a list of substrings at a separator. With no separator it splits on any whitespace.

Python 3 Since 3.0

Syntax

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

Parameters

ParameterDescription
sep optional The separator string. If omitted, split on runs of whitespace and drop empty strings. Default: None
maxsplit optional Maximum number of splits; the rest stays in the last item. Default: -1

Return value

A list of strings.

Examples

Whitespace vs an explicit separatorTry it yourself
print("the quick  brown fox".split())
print("a,b,,c".split(","))

Output

['the', 'quick', 'brown', 'fox']
['a', 'b', '', 'c']
Split and unpackTry it yourself
print("2026-10-22".split("-"))
year, month, day = map(int, "2026-10-22".split("-"))
print(year, month, day)

Output

['2026', '10', '22']
2026 10 22
Limit the number of splitsTry it yourself
print("key=value=with=equals".split("=", 1))

Output

['key', 'value=with=equals']

Common mistakes

  • split() and split(" ") differ: the first collapses multiple spaces and ignores leading/trailing space, the second keeps empty strings: "a b".split(" ") is ['a', '', 'b'].
  • To split on several different characters, use re.split().
  • For CSV data with quoted commas, use the csv module instead.

Version history

VersionChange
Python 3.0Added