str.split
Split the string into parts by a separator.
Python 3.13
str.split(sep=None, maxsplit=-1)Parameters
- sepstr | Noneoptional
- The separator; if not given, the string is split by spaces, tabs and newlines.
- maxsplitintoptional
- The maximum number of splits; -1 means no limit.
Return value
A list of strings; the separator is not included.
Examples
Splitting by a space
Python 3.13
print('hello world'.split(' '))['hello', 'world']
Splitting by any whitespace
Python 3.13
print(' one two '.split())['one', 'two']
Empty parts between separators
Python 3.13
print('a,,b'.split(','))['a', '', 'b']
Only the first split
Python 3.13
print('key=value=1'.split('=', 1))['key', 'value=1']
Details
Without sep, consecutive spaces count as one separator and spaces at the edges are dropped. With an explicit sep, empty parts are kept. That is why an empty string gives different results: "".split() gives [], and "".split(",") gives [""].
