Split a string with multiple delimiters in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Split a string with multiple delimiters in Python

To split a string with multiple delimiters:

  1. Use the re.split() method, e.g. re.split(r',|-', my_str).
  2. The re.split() method will split the string on all occurrences of one of the delimiters.
main.py
import re # ๐Ÿ‘‡๏ธ split string with 2 delimiters my_str = 'bobby,hadz-dot,com' my_list = re.split(r',|-', my_str) # ๐Ÿ‘ˆ๏ธ split on comma or hyphen print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'dot', 'com']

split string with multiple delimiters

The code for this article is available on GitHub

The re.split() method takes a pattern and a string and splits the string on each occurrence of the pattern.

The pipe | character is an OR. Either match A or B.

The example splits a string using 2 delimiters - a comma and a hyphen.

main.py
# ๐Ÿ‘‡๏ธ split string with 3 delimiters my_str = 'bobby,hadz-dot:com' my_list = re.split(r',|-|:', my_str) # ๐Ÿ‘ˆ๏ธ comma, hyphen or colon print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'dot', 'com']

Here is an example that splits the string using 3 delimiters - a comma, a hyphen and a colon.

You can use as many | characters as necessary in your regular expression.

# Split a string based on multiple delimiters using square brackets []

Alternatively, you can use square brackets [] to indicate a set of characters.

main.py
import re my_str = 'bobby,hadz-dot,com' my_list = re.split(r'[,-]', my_str) print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'dot', 'com']

split multiple delimiters

The code for this article is available on GitHub

Make sure to add all of the delimiters between the square brackets.

main.py
import re # ๐Ÿ‘‡๏ธ split string with 3 delimiters my_str = 'bobby,hadz-dot:com' my_list = re.split(r'[,-:]', my_str) print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'dot', 'com']

place all delimiters between brackets

You might get empty string values in the output list if the string starts with or ends with one of the delimiters.

# Handling leading or trailing delimiters

You can use a list comprehension to remove any empty strings from the list.

main.py
import re # ๐Ÿ‘‡๏ธ split string with 3 delimiters my_str = ',bobby,hadz-dot:com:' my_list = [ item for item in re.split(r'[,-:]', my_str) if item ] print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'dot', 'com']

exclude empty strings from result

The code for this article is available on GitHub

The list comprehension takes care of removing the empty strings from the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

An alternative approach is to use the str.replace() method.

# Split a string with multiple delimiters using str.replace()

This is a two-step process:

  1. Use the str.replace() method to replace the first delimiter with the second.
  2. Use the str.split() method to split the string by the second delimiter.
main.py
my_str = 'bobby_hadz!dot_com' my_list = my_str.replace('_', '!').split('!') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'dot', 'com']

split multiple delimiters using replace

The code for this article is available on GitHub
This approach is only convenient when you have few delimiters you want to split on, e.g. 2.

First, we replace every occurrence of the first delimiter with the second, and then we split on the second delimiter.

The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

Note that the method doesn't change the original string. Strings are immutable in Python.

Here is another example.

main.py
my_str = 'bobby hadz, dot # com. abc' my_list = my_str.replace( ',', '').replace( '#', '').replace('.', '').split() print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'dot', 'com', 'abc']

split three delimiters using replace

The code for this article is available on GitHub

We used the str.replace() method to remove the punctuation before splitting the string on whitespace characters.

We used an empty string for the replacement because we wanted to remove the specified characters.

You can chain as many calls to the str.replace() method as necessary.

The last step is to use the str.split() method to split the string into a list of words.

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)
When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.
main.py
my_str = 'bobby hadz com' print(my_str.split()) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

If the separator is not found in the string, a list containing only 1 element is returned.

# Split a string based on multiple delimiters with a reusable function

If you need to split a string based on multiple delimiters often, define a reusable function.

main.py
import re def split_multiple(string, delimiters): pattern = '|'.join(map(re.escape, delimiters)) return re.split(pattern, string) my_str = 'bobby,hadz-dot:com' print(split_multiple(my_str, [',', '-', ':']))

split based on multiple delimiters with function

The code for this article is available on GitHub

The split_multiple function takes a string and a list of delimiters and splits the string on the delimiters.

The str.join() method is used to join the delimiters with a pipe | separator.

main.py
# ๐Ÿ‘‡๏ธ ,|-|: print('|'.join([',', '-', ':']))

This creates a regex pattern that we can use to split the string based on the specified delimiters.

If you need to split a string into a list of words with multiple delimiters, you can also use the re.findall() method.

Want to learn more about splitting strings in Python? Check out these resources: Split a String and remove the Whitespace in Python,Split a String into multiple Variables in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev