Python string split() method
Cut a string into a list of pieces.
Syntax
str.split(sep=None, maxsplit=-1)
With no separator it splits on any run of spaces, tabs and new lines and ignores spaces at the ends: ideal for splitting text into words. With a separator such as "," it cuts at every occurrence, keeping empty pieces. maxsplit limits the number of cuts.
Parameters
| sep | where to cut; default: any whitespace |
| maxsplit | the most cuts to make |
Return value
a list of strings
Example
Code
print("the quick brown fox".split()) print("a,b,,c".split(",")) print("2026-11-17".split("-")) print("key=value=more".split("=", 1)) name, city = "Sita;Brisbane".split(";") print(name, city)
Output
['the', 'quick', 'brown', 'fox'] ['a', 'b', '', 'c'] ['2026', '11', '17'] ['key', 'value=more'] Sita Brisbane
Good to know. The opposite is
"sep".join(list).Related
Learn it step by step in the free lesson Strings, with exercises that check your answers.