Python String split() Method

The Python string split() method breaks a string into smaller parts and returns those parts as a list.

Use it when you want to turn one string into multiple strings, such as:

  • splitting a sentence into words
  • splitting comma-separated text
  • breaking simple structured text into pieces

Quick example

text = "apple,banana,cherry"
parts = text.split(",")
print(parts)
# ['apple', 'banana', 'cherry']

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

What split() does

split() is a string method.

It:

  • divides a string into parts
  • returns a new list of strings
  • does not change the original string

Example:

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

print(text)
print(parts)

Output:

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

If you are new to string methods, see Python strings explained: basics and examples.

Basic syntax

string.split(separator, maxsplit)

Both arguments are optional:

  • separator tells Python where to split
  • maxsplit limits how many splits happen

If separator is not given, Python splits on whitespace.

Example:

text = "one two three"
print(text.split())

Output:

['one', 'two', 'three']

Using split() with no separator

When you call split() with no argument, Python splits on whitespace.

Whitespace includes:

  • spaces
  • tabs
  • new lines

Python also treats repeated whitespace as a single separator.

Example:

text = "Python   is\tfun\nand useful"
parts = text.split()

print(parts)

Output:

['Python', 'is', 'fun', 'and', 'useful']

This is useful when you want simple word splitting.

split() vs split(' ')

These are not the same.

text = "a  b   c"

print(text.split())
print(text.split(" "))

Output:

['a', 'b', 'c']
['a', '', 'b', '', '', 'c']
  • split() uses any whitespace and ignores repeated spaces
  • split(" ") only splits on the space character and can create empty strings

If you need to clean extra spaces before or after text, Python string strip() method is often useful.

Using a custom separator

You can pass your own separator, such as ",", "-", or "::".

Python splits only where that exact separator appears.

Example with commas:

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

print(parts)

Output:

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

Example with a longer separator:

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

print(parts)

Output:

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

This is common when working with simple data formats.

For more task-focused examples, see how to split a string in Python.

Using maxsplit

maxsplit limits the number of splits.

After that limit is reached, the rest of the string stays together in the last item.

Example:

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

print(parts)

Output:

['name', 'age', 'city:country']

Here:

  • Python splits at the first :
  • then splits at the second :
  • then stops

This is useful when only the first few separators matter.

Another example:

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

Output:

['apple', 'banana cherry grape']

What split() returns

The result of split() is always a list.

Each item in that list is a string.

Example:

text = "cat,dog,bird"
parts = text.split(",")

print(parts)
print(type(parts))
print(parts[0])

Output:

['cat', 'dog', 'bird']
<class 'list'>
cat

Because the result is a list, you can:

  • loop over it
  • access items by index
  • combine items again later

If you want to turn the list back into one string, use Python string join() method.

Example:

parts = ['cat', 'dog', 'bird']
text = "-".join(parts)

print(text)

Output:

cat-dog-bird

Common edge cases

Here are some cases that often surprise beginners.

Splitting an empty string with no separator

text = ""
print(text.split())

Output:

[]

Splitting an empty string with a custom separator

text = ""
print(text.split(","))

Output:

['']

Separator not found

If Python does not find the separator, it returns a one-item list containing the original string.

text = "hello"
print(text.split(","))

Output:

['hello']

When to use split()

Use split() when you need to:

  • split a sentence into words
  • split comma-separated values
  • break lines or simple structured text into parts

It is especially useful for tasks like:

  • cleaning text
  • parsing user input
  • processing simple file content

Common mistakes

Beginners often run into these problems:

  • expecting split() to change the original string
  • forgetting that split() returns a list, not a string
  • using the wrong separator
  • assuming split(' ') behaves the same as split()
  • using an index that does not exist after splitting

Example of a common mistake:

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

print(text)

Output:

a,b,c

This happens because the result was not saved.

Correct version:

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

print(parts)

Output:

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

Another common problem is using an index that does not exist:

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

print(parts[2])

This causes an error because there is no third item. If you need help with that, see IndexError: list index out of range.

Useful debugging checks

If split() is not giving the result you expect, try printing these:

print(text)
print(text.split())
print(text.split(','))
print(len(text.split(',')))
print(type(text.split(',')))

These checks help you confirm:

  • what the original string looks like
  • which separator works
  • how many items were created
  • that the result is a list

FAQ

What is the difference between split() and split(' ')?

split() uses any whitespace and ignores repeated spaces.

split(' ') only splits on the space character and can produce empty strings.

Does split() change the original string?

No. Strings are immutable. split() returns a new list.

What does maxsplit do?

It sets the maximum number of splits. After that, the rest of the string stays in the final list item.

Why am I getting a list instead of a string?

Because split() always returns a list of strings.

See also