- How to Split String with Multiple Delimiters in Python
- Method 1: Using the re.split() method
- Example
- Method 2: Using functools.reduce() with list comprehension
- Example
- Method 3: Using the str.split() method with custom function
- Example
- Python String split() Method
- Definition and Usage
- Syntax
- Parameter Values
- More Examples
- Example
- Example
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- Split string with delimiter in python
- # Split a string with multiple delimiters in Python
- # Split a string based on multiple delimiters using square brackets []
- # Handling leading or trailing delimiters
- # Split a string with multiple delimiters using str.replace()
- # Split a string based on multiple delimiters with a reusable function
- # Additional Resources
How to Split String with Multiple Delimiters in Python
Here are the ways to split a string with multiple delimiters in Python.
- Using the “re.split()” method
- Using the “functools.reduce()” function with “list comprehension”
- Using the “str.split()” method with a custom function
Method 1: Using the re.split() method
To split a string with a single delimiter in Python, you can use the “re.split()” method. The re.split() method allows you to split a string based on a pattern, which can include multiple delimiters.
Example
import re input_string = "This is an;example,string:with.multiple|delimiters" delimiters = ";|,|:|\\.|\\|" split_string = re.split(delimiters, input_string) print(split_string)
['This is an', 'example', 'string', 'with', 'multiple', 'delimiters']
In this example, the input string is split into a list of substrings based on the specified delimiters.
Method 2: Using functools.reduce() with list comprehension
You can use the split() in combination with a for loop, functools.reduce() method, and list comprehension.
Example
from functools import reduce def multi_split(input_string, delimiters): if not delimiters: return [input_string] def _split_by_delimiter(string, delimiter): return [substring for segment in string for substring in segment.split(delimiter)] return reduce(_split_by_delimiter, delimiters, [input_string]) input_string = "This is an;example,string:with.multiple|delimiters" delimiters = [';', ',', ':', '.', '|'] split_string = multi_split(input_string, delimiters) print(split_string)
['This is an', 'example', 'string', 'with', 'multiple', 'delimiters']
Method 3: Using the str.split() method with custom function
The str.split() method is a built-in Python method for splitting a string into a list of substrings based on a specified delimiter. If the delimiter is not provided, the method splits the string based on whitespace characters (spaces, tabs, and newlines).
However, the str.split() method does not directly support multiple delimiters. To achieve this, you can create a custom function that uses the str.split() method in a loop over the delimiters.
Example
def multi_split(input_string, delimiters): segments = [input_string] for delimiter in delimiters: new_segments = [] for segment in segments: new_segments.extend(segment.split(delimiter)) segments = new_segments return segments input_string = "This is an;example,string:with.multiple|delimiters" delimiters = [';', ',', ':', '.', '|'] split_string = multi_split(input_string, delimiters) print(split_string)
['This is an', 'example', 'string', 'with', 'multiple', 'delimiters']
In this example, the multi_split() function accepts an input string and a list of delimiters. Then, it iteratively splits the input string using each delimiter and accumulates the result in the segments list.
Python String split() Method
Split a string into a list where each word is a list item:
txt = «welcome to the jungle»
Definition and Usage
The split() method splits a string into a list.
You can specify the separator, default separator is any whitespace.
Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Syntax
Parameter Values
Parameter | Description |
---|---|
separator | Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator |
maxsplit | Optional. Specifies how many splits to do. Default value is -1, which is «all occurrences» |
More Examples
Example
Split the string, using comma, followed by a space, as a separator:
txt = «hello, my name is Peter, I am 26 years old»
Example
Use a hash character as a separator:
Example
Split the string into a list with max 2 items:
# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split(«#», 1)
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Split string with delimiter in python
Last updated: Feb 24, 2023
Reading time · 4 min
# Split a string with multiple delimiters in Python
To split a string with multiple delimiters:
- Use the re.split() method, e.g. re.split(r’,|-‘, my_str) .
- The re.split() method will split the string on all occurrences of one of the delimiters.
Copied!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']
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.
Copied!# 👇️ 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.
Copied!import re my_str = 'bobby,hadz-dot,com' my_list = re.split(r'[,-]', my_str) print(my_list) # 👉️ ['bobby', 'hadz', 'dot', 'com']
Make sure to add all of the delimiters between the square brackets.
Copied!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']
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.
Copied!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']
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:
- Use the str.replace() method to replace the first delimiter with the second.
- Use the str.split() method to split the string by the second delimiter.
Copied!my_str = 'bobby_hadz!dot_com' my_list = my_str.replace('_', '!').split('!') print(my_list) # 👉️ ['bobby', 'hadz', 'dot', 'com']
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:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
Note that the method doesn’t change the original string. Strings are immutable in Python.
Copied!my_str = 'bobby hadz, dot # com. abc' my_list = my_str.replace( ',', '').replace( '#', '').replace('.', '').split() print(my_list) # 👉️ ['bobby', 'hadz', 'dot', 'com', 'abc']
We used the str.replace() method to remove the punctuation before splitting the string on whitespace 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:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At 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.
Copied!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.
Copied!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, [',', '-', ':']))
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.
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.
# 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.