You are currently viewing Split the Python String Based on Multiple Delimiters

How to split a string based on multiple delimiters in Python? You can use the re.split() function of re module allows you to split a string based on multiple delimiters specified by a regular expression pattern. This can be incredibly useful when you need more complex or flexible splitting logic than what is provided by the built-in split() method of strings. str.split() method that is commonly used to split a string into a list of substrings based on a specified delimiter.

Advertisements

You can split a string on multiple delimiters in Python using many ways, for example, by using the re.split(), re.findall(), re.split(), translate() & maketrans(), replace(), and split() functions. In this article, I will explain how to split a string on multiple delimiters by using all these methods with examples.

1. Quick Examples of Splitting String Over the Multiple Delimiters

If you are in a hurry, below are some quick examples of how to split the string on multiple delimiters.


# Quick examples of splitting the string on multiple delimiters

import re
# Initialize the string
string = "Hello| Welcome,to;SparkBy| Examples"

# Example 1: Split the string on multiple delimiters
# Using re.split()
pattern = r'[,;|]'
result = re.split(pattern, string)

# Example 2: Using re.findall() function
# split the string on multiple delimiters
result = re.findall(r"[\w']+", string)

# Example 3: Using re.split() function 
# with Expression Pattern ', |_|-|!' 
# String split multiple delimiters
string = "Welcome, to_spark-by ! Examples"
result = re.split(',|_|-|!', string)

# Example 4: Using custom logic
# Split the string on multiple delimiters
delimiters = ['|', ',', ';']
result = [string]
for delimiter in delimiters:
    temp_result = []
    for item in result:
        temp_result.extend(item.split(delimiter))
    result = temp_result

# Example 5: Using split() function
# Split string multiple delimiters
delimiters = [",", "|", ";"]
for delimiter in delimiters:
    string = " ".join(string.split(delimiter))
result = string.split()

# Example 6: Using translate() and maketrans() methods
delimiters = '|;,'
translation_table = str.maketrans(delimiters, ' ' * len(delimiters))
modified_string = string.translate(translation_table)
result = modified_string.split()

# Example 7: Using replace() method
# Replace delimiters with a common character
delimiters = ['|', ',', ';']
for delimiter in delimiters:
    string = string.replace(delimiter, ' ')
result = string.split()

2. Using the re module Split the String Based on Multiple Delimiters

You can use some of the functions of the Python re module(Regular Expressions) to split the strings based on multiple delimiters.

2.1 Using re.split() Function

The re.split() function is one of the functions of re module which is used to split a string over the multiple delimiters along with the defined pattern. This pattern r'[,;|]' matches any comma(,) , semicolon(;) , or pipe(|) character in the input string.

If you want to work with Regular Expressions you need to import re module. First, initialize a string string with the value "Hello| Welcome,to;SparkBy| Examples" and define a regular expression pattern r'[,;|]'. Pass the string and pattern into split() function.

This function returns a list of substrings. You print the result, which should be a list of substrings after splitting the original string using the specified delimiters.


import re
# Split the string on multiple delimiters
# Using re.split()
# Initialize the string
string = "Hello| Welcome,to;SparkBy| Examples"
print("Original string:\n", string)

# Define a regular expression 
# Pattern with multiple delimiters
pattern = r'[,;|]'
result = re.split(pattern, string)
print("After splitting the string on multiple delimiters:\n", result)

Yields below output.

Python string split multiple delimiters

2.2 Using re.split() Function with Expression Pattern ‘,|_|-|!’

Alternatively, you can use the re.split() function of re-module to split a string over the multiple delimiters. It looks like you have a string that you want to split based on delimiters such as comma (,), underscore (_), hyphen(-), and exclamation mark (!).

In the below example, first, you import the re module for regular expressions. You can initialize a string string with the value "Welcome, to_spark-by ! Examples". You can use the re.split() function with a regular expression pattern ',|_|-|!' to split the string on the specified delimiters. The pipe(|) character is used as an “OR” operator in the regular expression, allowing you to match any of the delimiters. This function returns a list of substrings. You print the result, which should be the list of substrings after splitting the original string using the specified delimiters.


import re

# Initialization the string
string = "Hello; Welcome, to_spark-by ! Examples"
print("Original string:\n", string)

# Using re.split() function
# String split multiple delimiters
result = re.split(';|,|_|-|!', string)
print("After splitting the string on multiple delimiters:\n", result)

# Output:
# Original string:
# Hello; Welcome, to_spark-by ! Examples
# After splitting the string on multiple delimiters: 
# ['Hello', ' Welcome', ' to', 'spark', 'by ', ' Examples']

2.3 Using re.findall() Function

Similarly, you can use the re.findall() function with a regular expression to split a string using multiple delimiters. This expression r"[\w']+" matches one or more word characters or apostrophes, effectively splitting the string at non-alphanumeric characters.

In the below example, you import the re module for regular expressions. You can initialize a string string with the value "Hello| Welcome,to;SparkBy|Examples". You can use this function with the regular expression pattern r"[\w']+" to find and extract substrings that consist of one or more word characters or apostrophes. You print the result, which should be a list of substrings after splitting the original string using the specified delimiters.


import re

# Initialization the string
string = "Hello| Welcome,to;SparkBy|Examples"
print("Original string:\n", string)

# Using re.findall() function
# String split multiple delimiters
result = re.findall(r"[\w']+", string)
print("After splitting the string on multiple delimiters:\n", result)

3. Using Custom Logic

You can also use custom logic to split the string over multiple delimiters. Here’s an example using a nested for loop to split the string over the multiple delimiters. First, initialize a string, a list of delimiters, and then use custom logic to split the string using each delimiter. The result is a list of split parts, where each part represents a segment of the original string separated by the specified delimiters.


# Split the string on multiple delimiters 
# Using custom logic
delimiters = ['|', ',', ';']
result = [string]
for delimiter in delimiters: 
    temp_result = []
    for item in result:
        temp_result.extend(item.split(delimiter))
    result = temp_result
print("After splitting the string on multiple delimiters:\n", result)

Yields the same output as above.

4. Split String on Multiple Delimiters Using Split() Function

You can also use the split() function to split the string based on multiple delimiters using. It involves using a loop to iterate through the delimiters and then repeatedly splitting and joining the string. Finally, you split the resulting string to get the individual parts.


# Using split() function
# Split string on multiple delimiters 
delimiters = [",", "|", ";"]
for delimiter in delimiters:
    string = " ".join(string.split(delimiter)) 
result = string.split()
print("After splitting the string on multiple delimiters:\n", result)

Yields the same output as above.

5. Using translate() and maketrans() Methods

You can also use the str.translate() method in combination with str.maketrans() to replace multiple characters with a common delimiter and then split the resulting string using the split() method.

In the below example, you define the original string. You define a string of delimiters ('|;,') that you want to replace with spaces. You use str.maketrans(delimiters, ' ' * len(delimiters)) it to create a translation table that maps each delimiter character to a space character of the same length. You use the str.translate(translation_table) method to replace the delimiters with spaces in the original string, creating the modified_string. You split the modified_string using the split() method to obtain the desired list of substrings.


# Initialization the string
string = "Hello| Welcome,to;SparkBy|Examples"
print("Original string:\n", string)

# Define a translation table to replace delimiters with spaces
delimiters = '|;,'
translation_table = str.maketrans(delimiters, ' ' * len(delimiters))

# Replace delimiters with spaces using translate()
modified_string = string.translate(translation_table)

# Split the modified string using split()
result = modified_string.split()
print("After splitting the string on multiple delimiters:\n", result)

Yields the same output as above.

6. Split The String on Multiple Delimiters Using replace() Method

Finally, You can split the string by multiple characters using the str.replace() method with str.split() to handle a series of delimiters. For example, first, define the original string and a list of delimiters that you want to replace with spaces. You can iterate through each delimiter in the list and use str.replace() function to replace them with spaces in the original string. You split the modified string using the split() method to obtain the desired list of substrings


# Initialization the string
string = "Hello| Welcome,to;SparkBy| Examples"
print("Original string:\n", string)

# Define a list of delimiters
delimiters = ['|', ',', ';']

# Using replace() method
# Replace delimiters with a common character
for delimiter in delimiters:
    string = string.replace(delimiter, ' ')

# Split the modified string using split()
result = string.split()
print("After splitting the string on multiple delimiters:\n", result)

Yields the same output as above.

Conclusion

In this article, I have explained how to split a string based on multiple delimiters in Python by using re.split(), re.findall(), re.Split(), translate() & maketrans(), replace(), and split() functions with examples.

Happy Learning !!