split() cuts a string into pieces and returns them as a list of strings. With no argument it splits on runs of whitespace, so "learn python".split() is ['learn', 'python'], and it ignores spaces at both ends.
Pass a separator to split at that exact text instead. The separator is not kept, and two separators in a row give an empty string between them. The optional maxsplit limits how many cuts are made. The opposite job, putting the pieces back together, is done by join().
text.split() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax
Call it with no arguments to split on whitespace, or pass a separator and an optional limit.
python
parts = text.split()
parts = text.split(sep)
parts = text.split(sep, maxsplit)
Call
What it does
"a b c".split()
Returns ['a', 'b', 'c']. Any run of whitespace is one separator.
"a,b,c".split(",")
Returns ['a', 'b', 'c']. The text is cut at every comma.
"a,b,c".split(",", 1)
Returns ['a', 'b,c']. Only one cut is made, so the rest stays together.
To cut from the right use rsplit(). To split into lines use splitlines(). To cut once and keep the separator, use partition().
Library declarations
python
str.split(sep=None, maxsplit=-1) -> list
# returns: a list of strings
# raises: ValueError if sep is an empty string
Parameters
Parameter
Description
sep
Optional. The text to split at. If it is omitted or None, the string is split on runs of whitespace. An empty string raises ValueError.
maxsplit
Optional. The most cuts to make, so the list has at most maxsplit + 1 items. Default -1, which means no limit.
Return Value
Return Value
Description
list
A list of strings. The separator is not included in the pieces, and the original string is not changed.
Python Examples
Example 1 — Break a Sentence Into Words
Call split() with no arguments and use the list it returns.
python
sentence = "learn python the easy way"
words = sentence.split()
print(words)
print(len(words))
print(words[0])
Output
['learn', 'python', 'the', 'easy', 'way']
5
learn
How It Works
No argument.split() cuts the sentence at each space and returns a list of five words.
A normal list.len(words) counts the pieces, which gives 5.
Use the pieces.words[0] is the first word, learn.
Example 2 — Empty Strings and Whitespace
See how a separator keeps empty pieces, and how split() without an argument does not.
No limit. Without maxsplit the text is cut at both equals signs, giving three pieces.
One cut. With maxsplit set to 1, only the first equals sign splits the text.
Unpack the pair. The list always has two items here, so it can be unpacked into key and value.
Empty input."".split(",") gives a list with one empty string, while "".split() gives an empty list.
Frequently Asked Questions
It breaks a string into a list of substrings. With no argument it splits on whitespace. With a separator it splits at that exact text.
split() treats any run of whitespace as one separator and ignores spaces at the ends. split(" ") cuts at every single space, so extra spaces produce empty strings.
Pass maxsplit as 1, for example text.split("=", 1). The list then has at most two items. Use rsplit() to cut once from the right.
A separator that appears twice in a row, or at the start or the end, has nothing between the cuts, so an empty string is returned. Use split() with no argument, or filter the empty pieces.
Use the join() method on the separator, for example ",".join(parts).