String method

str.split()

Cuts a string into a list of pieces: into words by default, or at any separator you choose, such as a comma.

BeginnerEvery example below runs in your browser. Change the code and press Run.

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

ParameterWhat it is
sep
optional, default None
The text to cut at. It can be one character (",") or several (" - "). With None, any whitespace counts and empty pieces are dropped.
maxsplit
optional, 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

Pythoneditable
text = "learn python step by step"
words = text.split()
print(words)
print(len(words), "words")
Output
['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.

Pythoneditable
line = "Maria,Lisbon,34"
name, city, age = line.split(",")
print(name)
print(city)
print(int(age) + 1)
Output
Maria
Lisbon
35

Every piece is a string, so age needs int() before you can do arithmetic with it.

3Extra spaces: split() vs split(" ")

Pythoneditable
messy = "  apples   pears  "
print(messy.split())
print(messy.split(" "))
Output
['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:

Pythoneditable
entry = "2026-03-14 Payment received from client"
date, message = entry.split(" ", maxsplit=1)
print(date)
print(message)
Output
2026-03-14
Payment received from client

5Turn a list of numbers typed as text into a total

Pythoneditable
prices = "4.50; 12.00; 3.25"
total = sum(float(p) for p in prices.split(";"))
print(f"Total: {total:.2f}")
Output
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.

Pythoneditable
name = "Ana Silva"
name.split()
print(name)
parts = name.split()
print(parts)
Output
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.

Pythoneditable
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)
Output
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.

Spotted a mistake on this page? Tell us