. Advertisement .
..3..
. Advertisement .
..4..
Are you having difficulties learning how to Split a string with multiple delimiters in Python? Don’t worry about it! This article will outline the problem’s underlying cause and suggest some solutions. Let’s get going!
How to Split a string with multiple delimiters in Python?
Utilize split() with ‘|’
We shall split a string depending on several delimiters to obtain all substrings in a list of strings. When giving several delimiters to the split() function, we must separate each delimiter with a ‘|.’
Firstly, we have the Syntax:
re.split('demimiter1 | delimiter2 |.....',exp_string)
Two parameters are required.
The first parameter accepts several delimiters separated by “|,” and the input string is the second parameter.
import re
testStringMulDeli ="Hi all, We/ITtutoria will help you split a string with multiple delimiters - Python"
print("Original String: ",testStringMulDeli)
# split a string with multiple delimiters
listOfStringMulDeli = re.split(',|/|-', testStringMulDeli)
print("Result String: ", listOfStringMulDeli)
The output of the command above, which splits the delimiter into four separate strings, is as follows:
Original String: Hi all, We/ITtutoria will help you split a string with multiple delimiters - Python
Result String: ['Hi all', ' We', 'ITtutoria will help you split a string with multiple delimiters ', ' Python']
Utilize split() with ‘[]’
With the help of the built-in Python method .split(), you can divide a string into separate parts using a specific delimiter.
This method will achieve the same effect as method number 2. As a result, we may include a semicolon, comma, and additional space [;,] in square brackets when providing the pattern of our regular expression. This will produce regular expressions. Take a look at the Syntax below:
re.split(r'[delimiter1,delimiter2,............]\s*',inp_str)
It requires two parameters.
Within [], the first parameter accepts multiple delimiters separated by ‘,’. The input string is the second parameter.
Applying the mentioned technique to an actual script
import re
testStringMulDeli = "Hi all, We/ITtutoria will help you split a string with multiple delimiters - Python"
listOfStringMulDeli = re.split("[;,-]", testStringMulDeli)
print("Original String: ",testStringMulDeli)
print("Result String: ", listOfStringMulDeli)
Output:
Original String: Hi all, We/ITtutoria will help you split a string with multiple delimiters - Python
Result String: ['Hi all', ' We/ITtutoria will help you split a string with multiple delimiters ', ' Python']
As you can see, the command has been split into multiple new values.
Use split() and replace()
You can see how to split a Python string with multiple delimiters by first replacing values in the illustration below. We’ll use our freshly formed string to replace all existing delimiters with a single, consistent delimiter
Let’s look at this:
exp_str = 'Hello! welcome, to Ittutoria.net'
split_char = exp_str.replace('!', ',').replace('.', ',')
splitted_str = split_char.split(',')
print(splitted_str)
Output:
['Hello', ' welcome', ' to Ittutoria', 'net']
This strategy works perfectly when you only have a few delimiters, but once you have more than three delimiters, it rapidly gets clumsy. You should take a close look at your situation and decide which solution will work best in that case.
Conclusion
We hope the information on this website will help you Split a string with multiple delimiters in Python. We also hope this clarifies the difficulty you ran into and, hopefully, offers a few more ways to prevent it. Please comment if you have any additional problems, and we’ll get back to you as soon as possible! We appreciate your reading.
Read more
Leave a comment