October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Python split(): Why It Behaves Differently With and Without a Separator

Python’s split() changes behavior depending on whether you omit the separator: whitespace runs collapse, while explicit delimiters preserve empty fields.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’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”.

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

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.

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 [].

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.

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

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.

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

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.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.