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
| Parameter | Description |
|---|---|
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
print("the quick brown fox".split())
print("a,b,,c".split(","))
Output
['the', 'quick', 'brown', 'fox'] ['a', 'b', '', 'c']
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
print("key=value=with=equals".split("=", 1))
Output
['key', 'value=with=equals']
Common mistakes
split()andsplit(" ")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
csvmodule instead.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.rsplit()· Split the string into a list from the right. With maxsplit, only the last few splits are made.str.splitlines()· Split the string into a list of lines at line boundaries (\n, \r\n and others), without the line endings.str.join()· Join the strings of an iterable into one string, with this string as the separator between them.str.partition()· Split the string at the first occurrence of a separator into a 3-tuple: the part before, the separator, and the part after.- Python Strings · tutorial