Python
string manipulation
delimiters
split method
programming tutorial

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.

Browse interview questions

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.

python
1import re
2
3text = "alpha,beta;gamma | delta\tepsilon"
4parts = re.split(r"[,;|\s]+", text.strip())
5print(parts)

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.

python
1import re
2
3expr = "a+b-c*d"
4tokens = re.split(r"([+\-*])", expr)
5print(tokens)

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().

python
1text = "id:42|region;ca,active"
2translation = str.maketrans({":": ",", "|": ",", ";": ","})
3normalized = text.translate(translation)
4parts = [part for part in normalized.split(",") if part]
5print(parts)

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:

python
1import csv
2from io import StringIO
3
4line = 'name="Doe, Jane";role=admin;active=true'
5reader = csv.reader(StringIO(line), delimiter=';', quotechar='"')
6fields = next(reader)
7print(fields)

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.

python
1import re
2
3splitter = re.compile(r"[,;|\s]+")
4
5
6def parse_line(line: str) -> list[str]:
7    return [part for part in splitter.split(line.strip()) if part]
8
9
10rows = ["a,b;c", "x|y z", "p;q|r"]
11print([parse_line(row) for row in rows])

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.

python
1def parse_record(line: str):
2    parts = parse_line(line)
3    if len(parts) != 3:
4        raise ValueError(f"Expected 3 fields, got {len(parts)}")
5    return parts[0], int(parts[1]), parts[2].lower() == "true"
6
7
8print(parse_record("user1,42,true"))

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 csv or shlex when quoted fields are part of the format.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.