Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsPython’s str.split() has two distinct modes: omit the separator and it groups whitespace while dropping empty boundary fields; provide a separator and it matches that exact delimiter, preserving empty fields between repeated delimiters. Which mode you choose changes what your result means.
Why does split() behave differently with and without a separator?
With no argument—or with None—str.split() treats runs of whitespace as one boundary and ignores whitespace at the edges. For example, ' 1 2 3 '.split() returns ['1', '2', '3']. Both ''.split() and ' '.split() return [].
As an Amazon Associate I earn from qualifying purchases.
With an explicit separator, Python looks for that exact string. Consecutive matches are not grouped: '1,,2'.split(',') returns ['1', '', '2'], and '1,2,,3,'.split(',') returns ['1', '2', '', '3', '']. Even ''.split(',') returns ['']. The official Python 3.14 documentation summarizes the distinction: “If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings”.
Use whitespace mode when repeated spacing is incidental and you want tokens. Use an explicit separator when empty fields matter in a simple delimited string. This is literal splitting, not CSV parsing: quoted fields and escaping need a CSV-aware parser.
#1 Best Overall
What does maxsplit count?
maxsplit limits the number of boundaries consumed, not the number of output items. A result has at most maxsplit + 1 items.
| Mode | Example | Result |
|---|---|---|
| Explicit separator | '1,2,3'.split(',', maxsplit=1) |
['1', '2,3'] |
| Whitespace mode | '1 2 3'.split(maxsplit=1) |
['1', '2 3'] |
There is a special case when whitespace mode uses maxsplit=0: Python consumes leading whitespace only, leaving trailing whitespace attached to the remaining text. Thus ' foo '.split(maxsplit=0) returns ['foo ']. Empty and whitespace-only inputs still return [].
Rank #2
Is a multi-character separator treated as a pattern?
No. An explicit separator may contain multiple characters, but Python matches the whole string literally. '1<>2<>3<4'.split('<>') returns ['1', '2', '3<4']; the unmatched final < is not a delimiter by itself.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →To split on one of several alternative delimiters, use re.split() with an appropriate regular expression rather than treating a string passed to split() as a character set.
When should you use rsplit() or splitlines()?
Use rsplit() for rightmost boundaries
rsplit(sep, maxsplit) applies a limited number of splits from the right. Otherwise, its behavior matches split(). Choose it when the final occurrence or occurrences matter—for example, separating a filename-like string at its last delimiter—rather than when you want the first occurrences.
Use splitlines() for line boundaries
splitlines() recognizes more than n: its boundaries include r, rn, vertical tab, form feed, file, group and record separators, next line, and Unicode line and paragraph separators. A terminal line break does not add an empty final item: 'One linen'.splitlines() returns ['One line']. By contrast, 'Two linesn'.split('n') returns ['Two lines', '']. Pass keepends=True when the line-ending characters should remain in each result.
Does the same whitespace rule apply to bytes?
No. For bytes.split() and bytearray.split(), omitted-separator whitespace mode recognizes ASCII whitespace only. The str method uses Unicode string whitespace semantics. If the input is text decoded from a byte stream, choose the type and decoding step deliberately; the two methods do not necessarily tokenize the same characters.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Quick Recap
Best Value
How to choose the right split operation
- Whitespace-separated tokens where repeated spaces do not represent fields: use
text.split(). - A literal delimiter where adjacent or edge delimiters must preserve empty fields: use
text.split(sep). - Several alternative delimiters: use
re.split(). - Only the rightmost limited matches: use
text.rsplit(sep, maxsplit). - Text lines with varied line-boundary characters: use
text.splitlines().
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.




