The string split method in Python

Share
Copied to clipboard.
Series: Strings
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
2 min. read Watch as video Python 3.10—3.14

Let's talk about the string split method.

Breaking apart a string by a separator

If you need to break a string into smaller strings based on a separator, you can use the string split method:

>>> time = "1:19:48"
>>> time.split(":")
['1', '19', '48']

The separator you split by can be any string. It doesn't need to be just one character:

>>> graph = "A->B->C->D"
>>> graph.split("->")
['A', 'B', 'C', 'D']

Splitting by whitespace

Note that it's a little bit unusual to call the string split method on a single space character:

>>> langston = "Does it dry up\nlike a raisin in the sun?\n"
>>> langston.split(" ")
['Does', 'it', 'dry', 'up\nlike', 'a', 'raisin', 'in', 'the', 'sun?\n']

It's usually preferable to call split without any arguments at all:

>>> langston = "Does it dry up\nlike a raisin in the sun?\n"
>>> langston.split()
['Does', 'it', 'dry', 'up', 'like', 'a', 'raisin', 'in', 'the', 'sun?']

Calling the split with no arguments will split on any consecutive whitespace characters. So we're even splitting on a new line here in between up and like (up\nlike).

Also note that the split method without any arguments removes leading and trailing whitespace (note that the last element in the list is sun? rather than sun?\n).

There's one more split feature that's often overlooked: maxsplit.

Splitting a specific number of times

When calling split with a maxsplit value, Python will split the string a given number of times.

So here we're splitting this string on a pipe character (|) just one time:

>>> line = "Rubber duck|5|10"
>>> line.split("|", maxsplit=1)
['Rubber duck', '5|10']

This is especially handy when you only care about the first or the first couple occurrences of a separator within a string.

Splitting from the end of a string

If it's the last couple occurrences of a separator that you care about, you can use the string rsplit method instead:

>>> line.rsplit("|", maxsplit=1)
['Rubber duck|5', '10']

This splits from the right-hand side of the string. Or you can think of it as a reverse split, splitting from the end of the string.

So here we're splitting on the last occurrence of the pipe character.

The string split method versus regular expressions

With the exception of calling split without any arguments, there's no way to:

  1. Ignore repeated separators
  2. Split on multiple separators at the same time
  3. Remove leading or trailing separators from the ends of your string

If you need any of those features, you should look into regular expressions using Python's re module. Specifically, the re.split function may come in handy.

See string methods in Python for more information on Python's most useful string methods.

🚀
New to Python? Try Python Jumpstart!

Python Jumpstart is designed to help new Python programmers get up to speed quickly. Get hands-on practice with 50 bite-sized modules that build your Python skills step by step.

5 Keys to Python Success 🔑

Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!