How to Split a String in Python

If you want to break text into smaller pieces in Python, use string splitting methods.

This page shows you how to:

  • Split a string into a list
  • Choose the right separator
  • Work with spaces, commas, and line breaks
  • Understand what split() returns

Quick answer #

text = "apple,banana,cherry"
parts = text.split(",")
print(parts)

Output:

['apple', 'banana', 'cherry']

Use split() to turn one string into a list of smaller strings.

What this page helps you do #

After reading this page, you will be able to:

  • Split a string into a list
  • Choose the correct separator
  • Handle spaces, commas, and new lines
  • Understand what split() returns

Use split() with the default separator #

If you call split() with no argument, Python splits the string on whitespace.

Whitespace includes:

  • Spaces
  • Tabs
  • New lines

This is useful when you want to split text into words.

text = "Python is fun"
parts = text.split()

print(parts)

Output:

['Python', 'is', 'fun']

One useful detail is that extra spaces are handled automatically.

text = "Python   is    fun"
parts = text.split()

print(parts)

Output:

['Python', 'is', 'fun']

So if you want to split by spaces and ignore repeated spaces, split() with no argument is usually the best choice.

If you want a full method reference, see the Python string split() method.

Split using a specific character #

You can also pass a separator to split().

Common separators include:

  • ","
  • "-"
  • "|"

Split by comma #

text = "apple,banana,cherry"
parts = text.split(",")

print(parts)

Output:

['apple', 'banana', 'cherry']

Split by dash #

text = "2024-10-31"
parts = text.split("-")

print(parts)

Output:

['2024', '10', '31']

Split by pipe #

text = "red|green|blue"
parts = text.split("|")

print(parts)

Output:

['red', 'green', 'blue']

The separator must match the text exactly. If the string uses commas, splitting on ";" will not work.

text = "apple,banana,cherry"
parts = text.split(";")

print(parts)

Output:

['apple,banana,cherry']

Notice that the result is still a list. split() always returns a list of strings.

Limit the number of splits #

Sometimes you do not want to split everywhere. In that case, use this form:

text.split(separator, maxsplit)

maxsplit tells Python how many times to split.

text = "name:age:city"
parts = text.split(":", 1)

print(parts)

Output:

['name', 'age:city']

Only the first : is used. The rest of the text stays together in the last item.

This is helpful when only the first part matters.

text = "ERROR - file not found - line 42"
parts = text.split(" - ", 1)

print(parts)

Output:

['ERROR', 'file not found - line 42']

Split lines of text #

If your string has multiple lines, use splitlines().

This is often better than splitting on "\n" because it handles line breaks more cleanly.

text = "first line\nsecond line\nthird line"
lines = text.splitlines()

print(lines)

Output:

['first line', 'second line', 'third line']

This is useful for:

  • File content
  • Copied text
  • Multi-line input

Example:

text = """apple
banana
cherry"""

lines = text.splitlines()

print(lines)

Output:

['apple', 'banana', 'cherry']

What to do after splitting #

After splitting, you get a list. You can then work with the list in different ways.

Access items with indexes #

text = "apple,banana,cherry"
parts = text.split(",")

print(parts[0])
print(parts[1])

Output:

apple
banana

If you are new to this, see Python list indexing explained.

Loop through the list #

text = "apple,banana,cherry"
parts = text.split(",")

for item in parts:
    print(item)

Output:

apple
banana
cherry

Convert values if needed #

Split values start as strings. If you need numbers, convert them.

text = "10,20,30"
parts = text.split(",")

numbers = [int(item) for item in parts]
print(numbers)

Output:

[10, 20, 30]

Remove extra whitespace with strip() #

If your text has spaces around commas, those spaces stay in the result.

text = "apple, banana, cherry"
parts = text.split(",")

print(parts)

Output:

['apple', ' banana', ' cherry']

To clean each item, use strip():

text = "apple, banana, cherry"
parts = text.split(",")

clean_parts = [item.strip() for item in parts]
print(clean_parts)

Output:

['apple', 'banana', 'cherry']

You can learn more on the Python string strip() method and how to remove whitespace from a string in Python.

Common mistakes #

Here are some common problems beginners run into when splitting strings.

Using the wrong separator character #

If the separator does not match the text, Python will not split it.

text = "a,b,c"
parts = text.split(";")

print(parts)

Output:

['a,b,c']

Expecting split() to return a string #

split() returns a list, not a single string.

text = "a,b,c"
parts = text.split(",")

print(parts)
print(type(parts))

Output:

['a', 'b', 'c']
<class 'list'>

Forgetting that split(’,’) does not remove spaces #

text = "a, b, c"
parts = text.split(",")

print(parts)

Output:

['a', ' b', ' c']

Use strip() if needed.

Trying to access a list item that does not exist #

text = "apple,banana"
parts = text.split(",")

print(parts[2])

This causes an error because there is no third item.

To debug, print the list and check its length first:

text = "apple,banana"
parts = text.split(",")

print(text)
print(parts)
print(type(parts))
print(len(parts))
print(repr(text))

FAQ #

What does split() return in Python? #

It returns a list of strings.

What is the difference between split() and split(’,’)? #

split() uses whitespace by default. split(',') only splits where there is a comma.

How do I split a string by spaces and ignore extra spaces? #

Use split() with no argument.

How do I split text into lines? #

Use splitlines().

Why do my split values still have spaces? #

Because split(',') separates at commas but does not trim spaces. Use strip() on each item if needed.

See also #

Press Esc to close