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.
#1 Best Overall
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.
Rank #2
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:
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.
Best Value
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.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Quick Recap
- 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.




