Split string with multiple delimiters in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python's built-in str.split() handles one delimiter at a time, so if you need to split on several separators, you need a different strategy. In most cases, re.split() is the most direct solution, but for simple high-volume parsing or quoted text, other approaches can be better.
Use re.split() for Multiple Delimiters
The standard answer is to use a regular expression that describes all delimiters.
This pattern splits on commas, semicolons, pipes, and whitespace. The + matters because it collapses runs of consecutive delimiters into one split boundary, which avoids producing lots of empty strings.
Without the +, repeated delimiters often create tokens you did not intend to keep.
Keep the Delimiters When They Matter
Sometimes the separators themselves are meaningful, for example in a lightweight tokenizer. In that case, capture the delimiters.
Because the regex uses a capturing group, the operators appear in the output. This is useful for simple parsers and syntax-highlighting tools where the punctuation is part of the information.
Normalize Delimiters Before Splitting
If the delimiters are just a few literal characters, another good pattern is to translate them all into one canonical delimiter and then call split().
This avoids regex overhead and can be attractive in very simple data-cleaning jobs. It is less flexible than re.split(), but the intent is easy to read when the delimiter set is small.
Use the Right Parser for Quoted Data
Plain splitting breaks down when delimiters can appear inside quoted values. In that case, use a parser designed for the data format instead of trying to outsmart it with regex.
For CSV-like input:
If the input is shell-like, shlex may be more appropriate. The rule is simple: once quoting rules matter, plain split logic is no longer enough.
Precompile the Regex for Repeated Parsing
If you are parsing many strings, compile the regex once and reuse it.
Precompiling keeps the parsing logic centralized and avoids rebuilding the same pattern repeatedly.
Validate the Results Immediately
Splitting is usually the first step, not the whole job. Once you have tokens, validate count and convert types early.
Doing validation right after splitting makes malformed input fail close to the source rather than much later in business logic.
Common Pitfalls
The most common mistake is using plain str.split() and expecting it to understand several delimiters at once. Another is forgetting to collapse repeated separators, which creates lots of empty tokens. Developers also reach for regex even when the input is actually CSV-like or shell-like and needs quote-aware parsing. Finally, parsing code becomes harder to maintain when the regex is scattered inline everywhere instead of being compiled and reused in one place.
Summary
- Use
re.split()when one string must be split by several delimiter types. - Add
+in the regex when repeated delimiters should count as one boundary. - Use capture groups if the delimiters themselves need to be preserved.
- Consider delimiter normalization for simple, literal high-volume parsing.
- Switch to
csvorshlexwhen quoted fields are part of the format.
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.