Python Reference
str.split()
Break a string into a list of substrings. Default splits on whitespace.
Last updated: October 2026
split() breaks a string into a list of substrings. With no arguments, it splits on whitespace and removes empty strings.
Full definition: str.split(sep=None, maxsplit=-1) returns a list of substrings by breaking the string at each occurrence of sep. Pass an explicit separator to keep empty strings in the result.
🎬 Live Example
Five common split patterns. Click any button to see the output.
💡 split() with no args removes empty strings. With an explicit sep, empties are kept.
Basic Syntax
str.split(sep=None, maxsplit=-1)
# sep — separator (default: any whitespace)
# maxsplit — max number of splits (default: -1 = unlimited)
# Returns: list of str
# Does NOT modify the original string
🎯 Parameters
| Parameter | Default | Description |
|---|---|---|
sep | None | Separator string. None = any whitespace. |
maxsplit | -1 | Max splits. -1 = no limit. |
Split on Whitespace (Default)
"hello world".split()
# ['hello', 'world']
" hello world ".split()
# ['hello', 'world'] ← empties removed
"hello\tworld\nfoo".split()
# ['hello', 'world', 'foo']
Pro tip: str.split() with no args is the standard way to normalize whitespace-separated tokens.
Custom Separator
"a,b,c".split(",") # ['a', 'b', 'c']
"2026-10-11".split("-") # ['2026', '10', '11']
"a::b::c".split("::") # ['a', 'b', 'c']
# Empty strings kept
"a,,b".split(",") # ['a', '', 'b']
Note: With an explicit separator, empty strings are preserved. Use a list comprehension to filter: [x for x in s.split(",") if x].
Limit Splits with maxsplit
"a,b,c,d".split(",", maxsplit=1)
# ['a', 'b,c,d']
"a,b,c,d".split(",", maxsplit=2)
# ['a', 'b', 'c,d']
# Useful for parsing key=value pairs
"name=Alice".split("=", maxsplit=1)
# ['name', 'Alice']
split vs rsplit
"a,b,c,d".split(",", maxsplit=1)
# ['a', 'b,c,d'] ← splits from left
"a,b,c,d".rsplit(",", maxsplit=1)
# ['a,b,c', 'd'] ← splits from right
# Perfect for file extensions
"archive.tar.gz".rsplit(".", maxsplit=1)
# ['archive.tar', 'gz']
Pro tip: Use rsplit when you want to preserve the left side and only split the tail.
Real-World: Parse CSV Line
line = "Alice,30,Engineer,NYC"
name, age, role, city = line.split(",")
# name='Alice', age='30', role='Engineer', city='NYC'
# Or convert types:
name, age, role, city = line.split(",")
age = int(age)
🎯 split vs rsplit vs splitlines vs join
| Method | Direction | Best for |
|---|---|---|
split | Left → right | General tokenizing |
rsplit | Right → left | File extensions, last delimiter |
splitlines | Line boundaries | Handle \r\n, \n, \r |
join | Reverse of split | List → string |
⚠️ Common Mistakes
- Forgetting explicit sep keeps empties.
"a,,b".split(",")gives['a', '', 'b']. - Using split when you need splitlines.
split('\n')leaves\rcharacters on Windows text. - Splitting on an empty separator.
"abc".split("")raises ValueError. Uselist("abc")instead. - Assuming split modifies the string. Strings are immutable — split returns a new list.
- Not handling case sensitivity. Split is case-sensitive.
❓ Frequently Asked Questions
What does split() do in Python?
str.split() breaks a string into a list of substrings based on a separator. With no arguments, it splits on any whitespace and removes empty strings.
What is the default separator for split()?
With no arguments, split() uses any run of whitespace (spaces, tabs, newlines) as the separator and removes leading/trailing whitespace. Pass an explicit separator for different behavior.
What does maxsplit do in split()?
maxsplit limits the number of splits. split(',', maxsplit=1) splits at the first comma only, returning two parts regardless of how many commas exist.
What is the difference between split and rsplit?
split starts splitting from the left; rsplit starts from the right. This only matters when maxsplit is used. Without maxsplit, they produce identical results.
Does split modify the original string?
No. Strings are immutable in Python. split returns a new list; the original string is unchanged.