Skip to main content

How to Split a String in Python with split

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

str.split cuts a string into pieces wherever a separator appears and returns the pieces as a list. "2026-11-28".split("-") gives the year, the month and the day.

Syntax​

Syntax
<pieces> = <text>.split(<separator>)
<pieces> = <text>.split(<separator>, <maxsplit>)
<pieces> = <text>.split()
PartWhat it does
<separator>The text to cut at. It doesn't appear in the pieces.
<maxsplit>Optional. The most cuts to make; whatever is left stays together in the last piece.
.split() with no separatorCuts at any run of spaces, tabs and line breaks, and ignores them at both ends.
<pieces>A list of strings.

The Python documentation for str.split: "Return a list of the words in the string, using sep as the delimiter string."

The Example​

The example splits a date at its dashes, splits it again with maxsplit set to 1, splits a name that has a double space and a tab with no separator at all, and splits a list with an empty spot in it.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which splits 2026-11-28 at the dash, again with maxsplit 1, splits Ada Lovelace and Babbage with no argument, and splits a,b,,c at the comma.

main.py
date = "2026-11-28"

print(date.split("-"))
print(date.split("-", 1))
print("Ada Lovelace\tBabbage".split())
print("a,b,,c".split(","))

Step 2: Run It​

Terminal
uv run python main.py
Output
['2026', '11', '28']
['2026', '11-28']
['Ada', 'Lovelace', 'Babbage']
['a', 'b', '', 'c']

A zsh terminal where uv run python main.py prints the lists 2026, 11, 28, then 2026 and 11-28, then Ada, Lovelace, Babbage, then a, b, an empty string and c.

uv run python main.py runs the file with the Python that uv finds. The command is the same in zsh, bash and PowerShell.

What Happened​

  • split("-") cut the date at both dashes.
  • split("-", 1) made one cut only, and kept the rest, 11-28, together in the last piece.
  • split() with no argument treated the double space and the tab as single breaks.
  • With an explicit ",", the two commas side by side left an empty string, '', between them.

Common Mistakes​

MistakeWhat happensDo this instead
Passing an empty separator, split("")ValueError: empty separatorUse list(text) to get the single characters
Splitting on " " when the spaces varyDouble spaces leave empty strings in the listCall split() with no argument
Expecting maxsplit to drop the restThe rest stays in the last piece: "a b c d".split(maxsplit=1) gave ['a', 'b c d']Split fully and take what you need

Troubleshooting​

SymptomLikely causeFix
A list with one item, the whole stringThe separator doesn't occur in the stringCheck the separator
AttributeError: 'int' object has no attribute 'split'The value isn't a stringConvert it first with str(value)

FAQ​

Does split change the original string?​

No. Strings in Python never change; split returns a new list.

How do I get the pieces into separate variables?​

Unpack the list: year, month, day = "2026-11-28".split("-"). The number of names must match the number of pieces.

Conclusion​

text.split(separator) returns the pieces between the separators as a list. Use maxsplit to make fewer cuts, and call it with no argument to split on any whitespace.

Additional Resources​