Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › Python › Reference › split()

Python Reference

str.split()

Break a string into a list of substrings. Default splits on whitespace.

Last updated: October 2026

split() breaks a string into a list of substrings. With no arguments, it splits on whitespace and removes empty strings.

Full definition: str.split(sep=None, maxsplit=-1) returns a list of substrings by breaking the string at each occurrence of sep. Pass an explicit separator to keep empty strings in the result.

🎬 Live Example

Five common split patterns. Click any button to see the output.

Input:

                
Output:

            

💡 split() with no args removes empty strings. With an explicit sep, empties are kept.

📝

Basic Syntax

str.split(sep=None, maxsplit=-1)

# sep       — separator (default: any whitespace)
# maxsplit  — max number of splits (default: -1 = unlimited)
# Returns: list of str
# Does NOT modify the original string

🎯 Parameters

Parameter Default Description
sepNoneSeparator string. None = any whitespace.
maxsplit-1Max splits. -1 = no limit.
1

Split on Whitespace (Default)

"hello world".split()
# ['hello', 'world']

"  hello   world  ".split()
# ['hello', 'world']  ← empties removed

"hello\tworld\nfoo".split()
# ['hello', 'world', 'foo']

Pro tip: str.split() with no args is the standard way to normalize whitespace-separated tokens.

2

Custom Separator

"a,b,c".split(",")          # ['a', 'b', 'c']
"2026-10-11".split("-")      # ['2026', '10', '11']
"a::b::c".split("::")        # ['a', 'b', 'c']

# Empty strings kept
"a,,b".split(",")            # ['a', '', 'b']

Note: With an explicit separator, empty strings are preserved. Use a list comprehension to filter: [x for x in s.split(",") if x].

3

Limit Splits with maxsplit

"a,b,c,d".split(",", maxsplit=1)
# ['a', 'b,c,d']

"a,b,c,d".split(",", maxsplit=2)
# ['a', 'b', 'c,d']

# Useful for parsing key=value pairs
"name=Alice".split("=", maxsplit=1)
# ['name', 'Alice']
4

split vs rsplit

"a,b,c,d".split(",", maxsplit=1)
# ['a', 'b,c,d']  ← splits from left

"a,b,c,d".rsplit(",", maxsplit=1)
# ['a,b,c', 'd']  ← splits from right

# Perfect for file extensions
"archive.tar.gz".rsplit(".", maxsplit=1)
# ['archive.tar', 'gz']

Pro tip: Use rsplit when you want to preserve the left side and only split the tail.

5

Real-World: Parse CSV Line

line = "Alice,30,Engineer,NYC"
name, age, role, city = line.split(",")
# name='Alice', age='30', role='Engineer', city='NYC'

# Or convert types:
name, age, role, city = line.split(",")
age = int(age)

🎯 split vs rsplit vs splitlines vs join

Method Direction Best for
splitLeft → rightGeneral tokenizing
rsplitRight → leftFile extensions, last delimiter
splitlinesLine boundariesHandle \r\n, \n, \r
joinReverse of splitList → string

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does split() do in Python?

str.split() breaks a string into a list of substrings based on a separator. With no arguments, it splits on any whitespace and removes empty strings.

What is the default separator for split()?

With no arguments, split() uses any run of whitespace (spaces, tabs, newlines) as the separator and removes leading/trailing whitespace. Pass an explicit separator for different behavior.

What does maxsplit do in split()?

maxsplit limits the number of splits. split(',', maxsplit=1) splits at the first comma only, returning two parts regardless of how many commas exist.

What is the difference between split and rsplit?

split starts splitting from the left; rsplit starts from the right. This only matters when maxsplit is used. Without maxsplit, they produce identical results.

Does split modify the original string?

No. Strings are immutable in Python. split returns a new list; the original string is unchanged.

🎯 What's Next?

← All Python Reference