In this Python tutorial, you will learn how to split a string with str.split(), choose a separator, limit the number of splits with maxsplit, and handle common cases such as repeated whitespace, comma-separated values, and empty fields.

Python split() Method for Splitting Strings

Use the Python split() string method when you want to divide one string into smaller strings. The method returns a new list containing the resulting parts.

The original string is not changed because Python strings are immutable.

Syntax of Python str.split()

The syntax of split() is:

</>
Copy
 string.split(separator, max)

In Python documentation and most modern code, these parameters are usually referred to as sep and maxsplit. The existing syntax above uses the names separator and max, but their meaning is the same.

ParameterRequired?Description
separatorOptionalThe delimiter used to divide the string. When omitted or set to None, Python splits on runs of whitespace.
maxOptionalThe maximum number of splits to perform. The returned list can contain at most max + 1 items. When omitted or negative, all possible splits are performed.

Return Value of split()

split() returns a Python list of strings. It does not return an array object and it does not modify the source string.

Python Split String Examples

The following examples show how split() behaves with whitespace, single-character delimiters, multi-character delimiters, delimiters at string boundaries, and a maximum number of splits.

1. Split a Python String on Whitespace

When no separator is passed, split() treats consecutive whitespace characters as one separator. Leading and trailing whitespace is ignored.

Example.py

</>
Copy
#a string
str = "Welcome to Python Tutorial by TutorialKart"

#split with no separator passed
parts = str.split()

print(parts)

Output

['Welcome', 'to', 'Python', 'Tutorial', 'by', 'TutorialKart']

You can also pass a literal space character as the separator.

Example.py

</>
Copy
#a string
str = "Welcome to Python Tutorial by TutorialKart"

#split with single space as separator
parts = str.split(' ')

print(parts)

Output

['Welcome', 'to', 'Python', 'Tutorial', 'by', 'TutorialKart']

Although both examples produce the same output for this input, split() and split(' ') are not always equivalent. A literal space separator can produce empty strings when spaces are repeated.

</>
Copy
text = "Python   split"

print(text.split())
print(text.split(' '))

Output

['Python', 'split']
['Python', '', '', 'split']

2. Split a Comma-Separated String in Python

Pass a comma as the separator to divide a comma-separated string into individual values.

Example.py

</>
Copy
#a string
str = "0.124,0.547,4.125,1.2,10.63"

#split with comma as separator
values = str.split(',')

print(values)

Output

['0.124', '0.547', '4.125', '1.2', '10.63']

The returned values are strings. Convert them explicitly when numeric values are required.

</>
Copy
text = "10,20,30"
numbers = [int(value) for value in text.split(',')]

print(numbers)

Output

[10, 20, 30]

For complete CSV files, use Python’s csv module instead of manually splitting lines, because quoted fields can contain commas.

3. Split with a Multi-Character Separator

The separator may contain more than one character. Python matches the complete separator string.

Example.py

</>
Copy
#a string
str = "hello---world---welcome---to---tutorialkart"

#split with another string as separator
values = str.split('---')

print(values)

Output

['hello', 'world', 'welcome', 'to', 'tutorialkart']

4. Split When the Separator Appears at the Start or End

When an explicit separator appears at the beginning or end of a string, split() preserves the missing value as an empty string in the returned list.

Example.py

</>
Copy
#a string
str = "---hello---world---welcome---to---tutorialkart---"

#split with another string as separator
values = str.split('---')

print(values)

Output

['', 'hello', 'world', 'welcome', 'to', 'tutorialkart', '']

This behavior is useful when empty fields are meaningful. To remove empty strings, filter the result explicitly.

</>
Copy
text = "---hello---world---"
parts = [part for part in text.split('---') if part]

print(parts)

Output

['hello', 'world']

5. Limit String Splitting with maxsplit

When you specify the max parameter, the split() method processes only the first specified number of matching separators.

Example.py

</>
Copy
#a string
str = "hello---world---welcome---to---tutorialkart"

#split with another string as separator
values = str.split('---', 2)

print(values)

Output

['hello', 'world', 'welcome---to---tutorialkart']

Here, Python performs two splits, so the returned list contains three items. Everything after the second matched separator remains in the final item.

6. Split Only Once at the First Separator

Use maxsplit=1 when the string has a key and value separated by the first occurrence of a delimiter.

</>
Copy
record = "name=TutorialKart=Python"
key, value = record.split('=', 1)

print(key)
print(value)

Output

name
TutorialKart=Python

Important split() Behaviors and Errors

Splitting an Empty String

An empty string behaves differently depending on whether you provide a separator.

</>
Copy
print("".split())
print("".split(','))

Output

[]
['']

An Empty Separator Raises ValueError

The separator cannot be an empty string. Calling split('') raises ValueError.

</>
Copy
text = "Python"
text.split('')

Output

ValueError: empty separator

split() Does Not Accept Regular Expressions

str.split() uses a fixed string separator. To split on multiple delimiters or a pattern, use re.split() from Python’s regular expression module.

</>
Copy
import re

text = "red,green;blue"
parts = re.split(r"[,;]", text)

print(parts)

Output

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

Python split() and rsplit() Difference

split() starts splitting from the left when maxsplit limits the operation. rsplit() follows the same general rules but starts from the right.

</>
Copy
path = "folder/subfolder/file.txt"

print(path.split('/', 1))
print(path.rsplit('/', 1))

Output

['folder', 'subfolder/file.txt']
['folder/subfolder', 'file.txt']

Frequently Asked Questions about Python split()

How do I split a string by spaces in Python?

Call text.split() without an argument. It handles spaces, tabs, newlines, repeated whitespace, and leading or trailing whitespace.

How do I split a string only once?

Pass 1 as the second argument, for example text.split(':', 1). The result contains at most two items.

Why does split(‘ ‘) return empty strings?

With an explicit space separator, every space is treated as a delimiter. Consecutive spaces therefore create empty fields. Use split() without an argument when you want runs of whitespace treated as one separator.

How do I split a string into individual characters?

Use list(text), not text.split(''). An empty string is not a valid separator for split().

Python String Splitting Summary

Use str.split() to divide a string into a list. Omit the separator for whitespace-aware splitting, pass a delimiter for exact matching, and use the second argument to limit how many splits are performed. Use rsplit() when the split limit should apply from the right, and re.split() when separators are defined by a pattern.