Use str.split() to break a string into a list of substrings. With no argument it splits on any run of whitespace; with a separator such as "," it splits on that exact text. Add maxsplit to limit the number of splits, use rsplit() to split from the right, and use re.split() when the delimiter is a pattern.
All of these return a list of strings and never modify the original string, because Python strings are immutable.
split()maxsplit and rsplit()re.split()split()Called without arguments, split() treats any run of spaces, tabs and newlines as one separator and drops empty strings at the start and end.
This is almost always what you want for splitting prose into words, and it is more robust than text.split(" ").
When you pass a separator, split() splits on every occurrence of that exact string and keeps empty fields.
The separator can be several characters long, but it cannot be an empty string: "abc".split("") raises ValueError. To split a string into characters, use list("abc").
maxsplit and rsplit()The second parameter, maxsplit, is the maximum number of splits, so the result has at most maxsplit + 1 items. rsplit() does the same from the right.
Notice that line.rsplit(" ", 2) gives three parts. For a single split at the first or last occurrence, partition() and rpartition() return a tuple of before, separator and after, and are easy to unpack.
re.split()When delimiters vary, use re.split(pattern, string, maxsplit=0). Note that in Python 3.13 and later, passing maxsplit positionally is deprecated, so pass it as a keyword.
If the pattern contains a capturing group, the matched separators are included in the result, as the third example shows.
| Goal | Tool | Example result |
|---|---|---|
| Split into lines | str.splitlines() | "a\nb".splitlines() gives ['a', 'b'] |
| Split by a pattern | re.split() | See above |
| Parse CSV with quotes | csv.reader | Handles "a,b" as one field |
| Split a file path | pathlib.PurePath.parts | ('/', 'usr', 'bin') |
| Split into fixed chunks | Slicing | s[i:i+3] in a loop |
Do not use split(",") for real CSV data, because quoted fields can contain commas. Use the csv module instead.
Input from input() is a string, so convert the parts after splitting and strip stray spaces.
For input 3, 4,,5, this prints [3, 4, 5]. Wrap the conversion in try and except ValueError if the data comes from users.
Use list(text). The split() method cannot do this because it does not accept an empty separator.
split() and split(" ")?split() collapses any run of whitespace and ignores leading and trailing space. split(" ") splits on every single space and returns empty strings for consecutive spaces.
Use re.split() with a character class, for example re.split(r"[,;|]", text).
Use text.splitlines(). It handles \n, \r\n and other line boundaries, unlike split("\n").
split() return empty strings?A separator at the start, end or twice in a row produces empty fields, for example ",a,,b".split(",") gives ['', 'a', '', 'b']. Filter them with [p for p in parts if p] when you do not need them.
Use split() for whitespace and fixed delimiters, maxsplit or rsplit() to limit the result, partition() for a single cut, splitlines() for lines and re.split() for patterns. For quoted CSV data use the csv module.