str.split()
str.split() breaks a string into a list of substrings around a separator. With no separator it splits on any run of whitespace and drops empty pieces; with one it cuts on that literal, and maxsplit caps how many cuts it makes.
Definition
str.split() divides a string into an ordered list of substrings, cutting it at each occurrence of a separator. It is the standard way to turn one string into many pieces: a comma-separated line into fields, a sentence into words, a path into segments. The original string is not modified — because strings are immutable, split() returns a brand-new list and leaves the string alone.
The separator is removed from the results — it marks *where* to cut and never appears in any piece. Its most important quirk is the default: called with no argument, split() treats any run of whitespace (spaces, tabs, newlines) as a single separator *and* discards empty leading, trailing and interior pieces. Pass an explicit separator and that convenience switches off, which is exactly the difference most bugs turn on. split() is the natural inverse of str.join().
Syntax
str.split(sep=None, maxsplit=-1)
# sep: the delimiter to cut on; None (default) means any run of whitespace
# maxsplit: cap on how many splits to make; -1 (default) means no limit
Parameters
| Parameter | Default | Description |
|---|---|---|
sep | None | The delimiter to split on. When None (or omitted), any run of consecutive whitespace is one separator and empty strings are dropped. When given a string, each single occurrence of that exact literal is a cut, and empty pieces are kept. |
maxsplit | -1 | The maximum number of splits to perform. -1 (the default) means no limit — split everywhere. A value of n makes at most n cuts, so the result has at most n + 1 elements, with the unsplit remainder as the final piece. |
The two parameters are independent: maxsplit caps the count whether or not you pass a sep. With the whitespace default, maxsplit still counts runs of whitespace as single separators, so "a b c".split(None, 1) yields ['a', 'b c'] — one cut, and the leftover keeps its internal spacing.
Return value
A new list of substrings, in order. The separators themselves are not included. The original string is unchanged. The result is always a list, even when no split happens.
- With no
sep, leading and trailing whitespace is ignored and empty pieces are dropped, so" a b ".split()is['a', 'b']. - With an explicit
sep, empty pieces are kept:"a,,b".split(",")is['a', '', 'b'], and a leading or trailing separator produces an empty-string element. - If
sepis not found in the string, the result is a single-element list holding the whole string. - Splitting an empty string with an explicit
sepreturns['']; with the whitespace default it returns[].
Examples
Default: split on any whitespace
line = " the quick brown fox "
print(line.split()) # ['the', 'quick', 'brown', 'fox']
Split on a literal separator
row = "name,age,city"
fields = row.split(",")
print(fields) # ['name', 'age', 'city']
print(len(fields)) # 3
sep=None vs sep=' ' on doubled spaces
text = "a b" # two spaces between a and b
print(text.split()) # ['a', 'b'] — empties dropped
print(text.split(" ")) # ['a', '', 'b'] — empty piece kept
Cap the cuts with maxsplit
path = "a/b/c/d"
print(path.split("/", 2)) # ['a', 'b', 'c/d'] — 2 cuts, remainder intact
rsplit: count from the right
path = "a/b/c/d"
print(path.rsplit("/", 1)) # ['a/b/c', 'd'] — split once, from the end
Split multi-line text into lines
log = "first line\nsecond line\nthird line"
lines = log.split("\n")
print(lines) # ['first line', 'second line', 'third line']
print(len(lines)) # 3
for n, row in enumerate(lines, start=1):
print(n, row.upper()) # 1 FIRST LINE / 2 SECOND LINE / 3 THIRD LINE
Peel a file extension with rsplit
filename = "archive.tar.gz"
name, ext = filename.rsplit(".", 1)
print(name) # 'archive.tar' — split only at the last dot
print(ext) # 'gz'
Parse one key=value pair, keeping the rest intact
setting = "url = https://x.com/a=b&c=d"
key, value = setting.split(" = ", 1)
print(key) # 'url'
print(value) # 'https://x.com/a=b&c=d' — later '=' left untouched
Split, transform, and rejoin
csv = "apple,banana,cherry"
upper = [w.title() for w in csv.split(",")]
print(upper) # ['Apple', 'Banana', 'Cherry']
print(" | ".join(upper)) # 'Apple | Banana | Cherry'
Splitting is usually the first step of a small pipeline: split() to get the pieces, then a comprehension or map over them. When you need the *last* separator rather than the first — peeling a filename off a path, say — rsplit() with a maxsplit of 1 is the natural counterpart, cutting from the right instead of the left.
Browser & runtime support
The whitespace-default gotcha
The behavior that catches people out is the difference between split() and split(' '). The bare call collapses any run of whitespace and throws away empty pieces, which is what you want for tokenising human text with irregular spacing. Passing an explicit single space turns that off: every space becomes its own cut, so two adjacent spaces yield an empty string between them. When you are parsing words from messy input, prefer the no-argument form; reserve an explicit separator for structured data where every delimiter genuinely marks a field, even an empty one.
Availability
str.split() is a core string method available in every version of Python 3 with no import required. Its companion rsplit() and the related splitlines() (which cuts on line boundaries) are equally standard and safe to rely on across every maintained interpreter.
Frequently asked questions
What is the difference between split() and split(' ') in Python?
split() with no argument treats any run of whitespace as one separator and drops empty pieces. split(' ') cuts on each single space, so doubled spaces produce empty-string elements.How do I split a string by a comma in Python?
s.split(','), which returns a list of the comma-separated pieces. Note that empty fields are kept, so "a,,b".split(',') returns ['a', '', 'b'].What does maxsplit do?
maxsplit caps the number of splits. s.split(sep, n) makes at most n cuts, so the result has at most n + 1 elements and the unsplit remainder is the last one.How is rsplit different from split?
rsplit() splits from the right. It only matters together with maxsplit: s.rsplit(sep, 1) cuts at the last separator instead of the first.