How to Split Strings with Multiple Delimiters in Python?

Recently in a Python webinar, a question was raised by someone on how to split strings with multiple delimiters in Python. After exploring various methods I found five effective ways to accomplish this task. Let us learn all the methods with suitable examples.

Split Strings with Multiple Delimiters in Python

In Python, you can split strings with multiple delimiters using different methods, Let us see all the important methods.

Read How to Convert Hexadecimal String to Integer in Python?

Method 1. Use re.split() Function

The re.split() function in Python allows you to split a string using multiple delimiters in one go.

Example: Split a USA address with multiple delimiters (,, ;, and |).

import re

address = "New York, NY; Los Angeles | San Francisco, CA"
cities = re.split(r"[,;|]\s*", address)  # Splitting by `,`, `;`, or `|` followed by optional spaces

print(cities)

Output:

['New York', 'NY', 'Los Angeles', 'San Francisco', 'CA']

I have executed the above example code and added the screenshot below.

Split Strings with Multiple Delimiters in Python

The regex pattern [ ,;|]\s* matches commas, semicolons, or pipes followed by spaces.

Check out How to Pad Strings with Spaces in Python?

Method 2. Use re.findall() Method

The re.findall() method in Python extracts words while ignoring delimiters.

Example: Extract USA phone numbers split by multiple delimiters.

import re

phone_numbers = "123-456-7890, 987.654.3210 | (555) 123 4567"
numbers = re.findall(r"\d{3}[-.\s]?\d{3}[-.\s]?\d{4}", phone_numbers)

print(numbers)

Output:

['123-456-7890', '987.654.3210']

I have executed the above example code and added the screenshot below.

Split Strings with Multiple Delimiters in Python re.findall()

The regex pattern \d{3}[-.\s]?\d{3}[-.\s]?\d{4} captures USA phone numbers with different separators (-, ., space).

Read How to remove specific words from a string in Python?

Method 3. Use translate() & maketrans() Methods

The translate() in Python method removes unwanted characters efficiently.

Example: Split a USA address with multiple delimiters (,, ;, and |).

state_list = "California, Texas; Florida | New York"
trans_table = str.maketrans(",;|", "   ")  # Replace `,`, `;`, `|` with spaces
cleaned_states = state_list.translate(trans_table).split()

print(cleaned_states)

Output:

['California', 'Texas', 'Florida', 'New York']

I have executed the above example code and added the screenshot below.

How to Split Strings with Multiple Delimiters in Python

maketrans(",;|", " ") replaces ,, ;, and | with spaces. .translate(trans_table) applies the mapping. .split() then removes extra spaces.

Check out Find the First Number in a String in Python

Method 4. Use replace() Method

You can use .replace() in Python to replace delimiters with spaces before splitting.

Example: Clean up ZIP codes separated by different delimiters.

zip_codes = "10001,90210; 33101 | 77001"
for delimiter in [",", ";", "|"]:
    zip_codes = zip_codes.replace(delimiter, " ")  # Replace each delimiter with a space

zip_list = zip_codes.split()  # Split using whitespace
print(zip_list)

Output:

['10001', '90210', '33101', '77001']

.replace(",", " "), .replace(";", " "), etc., replaces different delimiters with spaces..split() then separates words using whitespace.

Read How to Compare Strings in Python?

Method 5. Use split() Function

The split() function in Python can handle a single delimiter but can be used repeatedly.

Example: Split a list of USA cities with a single delimiter.

cities = "Chicago|Houston|Seattle|Miami"
city_list = cities.split("|")  # Using `|` as the delimiter

print(city_list)

Output:

['Chicago', 'Houston', 'Seattle', 'Miami']

split("|") breaks the string at every |.

Check out How to Create a String of N Characters in Python?

Conclusion

In this tutorial, I have explained how to split strings with multiple delimiters in Python. I covered mainly five methods to achieve this task such as using the re.split() function, re.findall() method, translate() and maketrans() methods, replace() method, and split() function.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.