Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Split Strings with Multiple Delimiters in Python

Use re.split() for multiple delimiters in Python: character classes handle single characters, while alternation handles multi-character tokens. See how to control captured separators, empty fields, and split limits.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s re.split() when a string may contain more than one delimiter. Put single-character delimiters in a character class, such as r"[,;|]"; use regex alternation for multi-character tokens. Choose the pattern based on whether delimiters or empty fields should remain in the result.

Choose the splitting method for your delimiter

Input pattern Use Example
One exact separator string str.split(sep) text.split(",")
Several one-character delimiters re.split() with a character class re.split(r"[,;|]", text)
Several multi-character delimiter tokens re.split() with alternation re.split(r"(?:END|STOP)", text)
General whitespace tokenization str.split() with no separator text.split()
General line boundaries str.splitlines() text.splitlines()

Use a regex only when its pattern expresses the delimiters you need. Python’s regular-expression reference defines re.split() as splitting at occurrences of a pattern; the built-in string methods are better suited to exact separators, whitespace, and line boundaries.

Split on multiple single-character delimiters

A character class matches any one character listed inside its brackets. For commas, semicolons, or vertical bars:

import re

text = "red,green;blue|yellow"
parts = re.split(r"[,;|]", text)
print(parts)
# ['red', 'green', 'blue', 'yellow']

This pattern treats each listed character as an independent delimiter. It does not treat a sequence such as || as one indivisible token; each matching bar is a delimiter. Use a different pattern if delimiters are multi-character tokens.

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.

Split on multi-character delimiter tokens

Use alternation inside a group when any of several complete tokens should split the text. A non-capturing group, written (?:...), groups the alternatives without returning the matched delimiter in the output:

import re

text = "alphaENDbetaSTOPgamma"
parts = re.split(r"(?:END|STOP)", text)
print(parts)
# ['alpha', 'beta', 'gamma']

Alternation matches one of the listed strings. If alternatives overlap, order can affect which alternative matches first, so place a longer token before a shorter token when that is the intended interpretation.

Understand captured delimiters and empty fields

Capturing groups return the separators

Parentheses in a regex are capturing by default. If the delimiter pattern contains a capturing group, re.split() includes the matched separator text in the result. Use (?:...) when grouping alternatives but excluding their text.

Leading, adjacent, and trailing delimiters

Splitting does not automatically discard empty fields. For example, re.split(r"[,;]", ",red;;blue,") produces ['', 'red', '', 'blue', '']: the leading delimiter, adjacent delimiters, and trailing delimiter each mark an empty field. Preserve these values if empty fields carry meaning, such as representing missing columns. If the data contract says to discard them, filter them explicitly:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
parts = [part for part in re.split(r"[,;]", text) if part]

That filter also removes fields containing only an empty string; it does not trim whitespace from non-empty fields. Apply strip() separately if whitespace around values should be removed.

Patterns that can match an empty string

A delimiter pattern that matches zero characters can split at boundaries or between characters, rather than only at visible separators. Avoid empty-matching patterns unless that behavior is deliberate; the Python regex documentation describes how empty matches interact with splitting.

Limit the number of splits with maxsplit

Pass maxsplit as a keyword argument to cap the number of delimiter matches. Any unsplit remainder stays in the final list element:

parts = re.split(r"[,;|]", "red,green;blue|yellow", maxsplit=2)
# ['red', 'green', 'blue|yellow']

Starting with Python 3.13, passing maxsplit or flags positionally is deprecated. Keyword arguments make the intended option clear and work with current Python versions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use string methods for whitespace and line endings

Whitespace-separated values

When the goal is to split on runs of whitespace—not on a chosen delimiter set—call str.split() without an argument. It treats consecutive whitespace as a separator and does not produce empty fields for runs at the edges.

Lines with different boundary characters

For line-oriented text, str.splitlines() recognizes more boundaries than just n, including r, rn, vertical tab, form feed, and additional Unicode line separators. It omits line endings by default; pass keepends=True to retain them. See the built-in types reference.

re.split(r"n+", text) is appropriate when one or more newline characters specifically define the delimiter. It does not cover the broader set of line boundaries handled by splitlines().

Write regex patterns clearly

Prefer raw string literals such as r"[,;|]" and r"s+". A backslash has meaning both in Python string literals and in regular expressions; the raw-string prefix reduces confusion when writing regex escapes. Raw strings do not change regex behavior—they make the pattern easier to read and maintain.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Use a character class for a set of individual delimiter characters.
  • Use alternation for complete multi-character tokens.
  • Use a non-capturing group when alternatives need grouping but the delimiter should not appear in the output.
  • Decide whether empty fields should be preserved before filtering results.
  • Use maxsplit=... when the remainder should stay together.

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 *

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.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair 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.