str.split()
Cuts a string into a list of pieces: into words by default, or at any separator you choose, such as a comma.
str.split(sep=None, maxsplit=-1)split() breaks a string into smaller strings and gives them back as a list. With no arguments it splits on whitespace, so a sentence becomes a list of words. Give it a separator, such as ",", and it cuts the text at every comma instead.
It is the usual first step when you read data from a text file, a CSV line or something a user typed: split the line, then work with the pieces.
One detail surprises people: split() with no separator and split(" ") are not the same. The first treats any run of spaces, tabs and new lines as one gap and ignores spaces at the ends. The second cuts at every single space, so two spaces in a row give you an empty string.
Parameters
| Parameter | What it is |
|---|---|
sepoptional, default None | The text to cut at. It can be one character (",") or several (" - "). With None, any whitespace counts and empty pieces are dropped. |
maxsplitoptional, default -1 | The most cuts to make. The rest of the string stays together in the last piece. -1 means no limit. |
Return value
A new list of strings. The original string does not change.
Examples
1Split a sentence into words
text = "learn python step by step"
words = text.split()
print(words)
print(len(words), "words")['learn', 'python', 'step', 'by', 'step'] 5 words
2Split a CSV line at commas
A line from a spreadsheet file usually holds its values separated by commas.
line = "Maria,Lisbon,34"
name, city, age = line.split(",")
print(name)
print(city)
print(int(age) + 1)Maria Lisbon 35
Every piece is a string, so age needs int() before you can do arithmetic with it.
3Extra spaces: split() vs split(" ")
messy = " apples pears "
print(messy.split())
print(messy.split(" "))['apples', 'pears'] ['', '', 'apples', '', '', 'pears', '', '']
Without a separator the extra spaces disappear. With " " every space is a cut, so you get empty strings.
4Only cut once with maxsplit
A log line has a date, then a message that itself contains spaces. Cut only at the first space:
entry = "2026-03-14 Payment received from client"
date, message = entry.split(" ", maxsplit=1)
print(date)
print(message)2026-03-14 Payment received from client
5Turn a list of numbers typed as text into a total
prices = "4.50; 12.00; 3.25"
total = sum(float(p) for p in prices.split(";"))
print(f"Total: {total:.2f}")Total: 19.75
float() ignores the spaces left around each number, so " 12.00" still works.
Common mistakes
Expecting split() to change the string
Strings in Python never change in place. split() returns a new list, so store it in a variable.
name = "Ana Silva"
name.split()
print(name)
parts = name.split()
print(parts)Ana Silva ['Ana', 'Silva']
Unpacking into the wrong number of variables
a, b = text.split(",") only works when there are exactly two pieces. Check the length first, or use maxsplit.
line = "red,green,blue"
try:
first, second = line.split(",")
except ValueError:
print("ValueError: 3 pieces, but only 2 names")
first, rest = line.split(",", maxsplit=1)
print(first, "|", rest)ValueError: 3 pieces, but only 2 names red | green,blue
Questions people ask
How do I split a string into single characters?
Use list(text): list("abc") gives ['a', 'b', 'c']. split() cannot do this, because an empty separator raises ValueError.
How do I split on several different separators?
Use re.split() from the re module: re.split(r"[,;]", "a,b;c") gives ['a', 'b', 'c'].
What is the difference between split() and rsplit()?
They only differ when you set maxsplit: rsplit() makes its cuts from the right end. "a.b.c".rsplit(".", 1) gives ['a.b', 'c'].
How do I join the pieces back together?
Use str.join(): " ".join(words) puts a space between every piece.