split() in Python: turning one string into many

The split method is how a single line of text becomes a list of separate values. It is small, it is everywhere, and it has two habits worth knowing.

Published 2026-09-06 · by M.R. Sterling

A huge amount of real data arrives as text with something between the pieces. A comma between fields. A space between words. A pipe between the parts of a message. The tool that turns that single string into separate, usable values is split(), and it is worth knowing well because you will use it constantly.

What split does

Call .split() on a string and it hands you back a list. Each item is a chunk of the original, with the separator removed.

"apple,banana,cherry".split(",") gives you a list of three strings: the fruits, without the commas.

You choose the separator by passing it in. A comma, a space, a tab, a pipe, whatever sits between your pieces.

Habit one: no argument means split on whitespace

If you call .split() with nothing inside the brackets, it splits on any run of whitespace and, importantly, ignores extra spaces. So a sentence with double spaces or a stray tab still comes apart into clean words with no empty pieces in between.

The moment you pass a specific separator, that friendly behaviour stops. "a,,b".split(",") gives you three items, and the middle one is an empty string, because there really was nothing between those two commas. That is not a bug. It is split telling you a field was empty, which is often exactly what you need to know.

Habit two: the result is always strings

Split never guesses types. If you split "10,20,30" you get the strings "10", "20" and "30", not the numbers. Do the maths before converting them and Python will either complain or, worse, join them as text. Convert each piece with int() or float() when you need a number.

The two classic split bugs are forgetting that an empty field becomes an empty string, and forgetting that every piece is still text. Neither is subtle once you have met it.

Reaching one piece

Because split returns a list, you get a piece by its position. parts[0] is the first, parts[1] the second. If you only want the first two pieces and want the rest left alone, split(",", 1) splits just once, giving you the first field and everything after it in one go. That second argument, the maximum number of splits, saves a lot of awkward rejoining.

Going the other way

The opposite of split is join. ",".join(parts) glues a list back into one string with commas between. Split to take data apart, join to put it back together. They are a matched pair, and real programs use both in the same breath.

Where to go next

Splitting text is the first real data-wrangling skill, and it opens straight into parsing files, messages and records. Chopping Data with split() is built entirely around this one method and its quirks, and Strings Up Close covers the wider set of string tools you will reach for right after it.

Books that go deeper

More articles

One email when a new book lands

No schedule, no filler. Just the new titles and the odd free chapter.