Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Python’s str.split() has two different behaviors depending on whether you omit sep or pass one explicitly. text.split() collapses whitespace runs and drops empty boundary fields; text.split(',') treats each comma as a literal delimiter and preserves empty fields between or around commas. Choosing the right form depends on whether repeated separators and blank fields carry meaning.

Why split() changes when you provide a separator

With no separator—or with None—split() uses whitespace mode. Consecutive whitespace acts as one boundary, and whitespace at the beginning or end does not produce empty items:

'   1   2   3   '.split()  # ['1', '2', '3']
''.split()                # []
'   '.split()             # []

This is useful for separating words or tokens when spacing is not meaningful. It does not preserve repeated spaces as empty columns.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When you pass a separator, Python looks for that exact delimiter. Repeated delimiters can produce empty strings, including at the ends. The Python 3.14 documentation puts it this way: “If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings.”

'1,,2'.split(',')       # ['1', '', '2']
'1,2,,3,'.split(',')    # ['1', '2', '', '3', '']
''.split(',')           # ['']

Use an explicit separator when empty fields matter in a simple delimited string. This method does not parse CSV quoting or escaping; use a CSV-aware parser for actual CSV data.

These behaviors are documented in the Python Software Foundation’s Python 3.14 built-in types reference.

How maxsplit affects the result

maxsplit limits the number of splits, not the number of returned items. A result can contain at most maxsplit + 1 items. The remaining unsplit text stays together in the final item.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
'1,2,3'.split(',', maxsplit=1)  # ['1', '2,3']
'1 2 3'.split(maxsplit=1)       # ['1', '2 3']

The zero case in whitespace mode is unusual: maxsplit=0 removes only leading whitespace. Whitespace at the end of the remaining item stays there.

'   foo   '.split(maxsplit=0)  # ['foo   ']
''.split(maxsplit=0)          # []
'   '.split(maxsplit=0)        # []

That special behavior applies to whitespace mode. With an explicit separator, a zero maximum means no splits are made.

Explicit separators are literal strings, not character sets

A separator can contain multiple characters, but Python matches it as one exact string. It does not treat each character in the separator as an alternative delimiter.

'1<>2<>3<4'.split('<>')  # ['1', '2', '3<4']

Here, <> is the delimiter; the leftover < does not match it. To split on several alternative delimiters, use re.split() from Python’s regular-expression module.

When to use rsplit() or splitlines()

Use rsplit() for limited splits from the right

rsplit(sep, maxsplit) follows the same delimiter rules as split(), but applies a limited number of splits from the right. This is useful when the rightmost delimiter is the one that matters:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
'a/b/c'.rsplit('/', maxsplit=1)  # ['a/b', 'c']

Use splitlines() for line boundaries

splitlines() recognizes more line boundaries than just n, including r, rn, vertical tab, form feed, several separators used in text formats, and Unicode line and paragraph separators. It also handles the end of the string differently from splitting on a literal newline: a final line break does not add an extra empty item.

'One linen'.splitlines()  # ['One line']
'Two linesn'.split('n') # ['Two lines', '']
''.splitlines()            # []

Pass keepends=True to retain the line-ending characters in each returned line.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

One difference between str and byte splitting

For bytes.split() and bytearray.split(), omitted-separator whitespace mode recognizes ASCII whitespace only. By contrast, str.split() uses Unicode string whitespace behavior. This matters when processing encoded data as bytes rather than decoded text.

The Python Software Foundation documents these string, binary-sequence, and related method behaviors in its Python 3.14 built-in types reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.