In this python tutorial, you will learn how to split a string with multiple delimiters.
Table Of Contents
Split a string with a single delimiter using split()
Here, we will split a string into multiple substring based on a single delimeter. The split() function is available in the re module. It splits a string into substrings and returns a list of strings separated by a delimiter.
Syntax:
re.split('delimiter',inp_str)
It takes two parameters.
- the first parameter is the delimiter
- the second parameter is the input string.
It splits the string into multiple substrings based on the delimeter and returns that list.
Example 1:
Frequently Asked:
- Extract Numbers from String in Python
- Replace all occurrences of a character in String in Python
- Split string at every Nth character in Python
- Python: How to convert integer to string (5 Ways)
In this example, we will have a string that has a comma(,) delimiter – “Welcome, to this Pointer, Python”. Now we will split this string based on comma.
import re # Consider the string inp_str= "Welcome, to this Pointer, Python" print("Actual String: ",inp_str) # Split the string with single delimiter. listOfstrs = re.split(',',inp_str) print("After Splitting the string with single delimiter: ", listOfstrs)
Output:
Actual String: Welcome, to this Pointer, Python After Splitting the string with single delimiter: ['Welcome', ' to this Pointer', ' Python']
We can see that string is split into three strings.
Example 2:
In this example, we have a string that has a hyphen(-) delimiter – “Welcome- to this Pointer- Python”. Now we will split this string.
import re # Consider the string inp_str= "Welcome- to this Pointer- Python" print("Actual String: ",inp_str) # Split the string with single delimiter. listOfstrs = re.split('-',inp_str) print("After Splitting the string with single delimiter: ", listOfstrs)
Output:
Actual String: Welcome- to this Pointer- Python After Splitting the string with single delimiter: ['Welcome', ' to this Pointer', ' Python']
We can see that string is split into three strings.
Split a string with multiple delimiters using split() with ‘|’
Here, we will split a string based on different delimiters and get all substrings in a list of strings. The split() function is available in the re module, so first we have to import that. To specify multiple delimiters, we have to separate each delimiter with ‘|’ while passing them into split() function.
Syntax:
re.split('demimiter1 | delimiter2 |.....',inp_str)
It takes two parameters.
1. the first parameter takes multiple delimiters separated by |.
2. the second parameter is the input string.
It splits the string based on multiple delimeters provided in the first argument and returns a list of substrings.
Example 1:
In this example, we have a string that has multiple delimiters – “Welcome, to this / Pointer – Python”. Now we will split this string based on three delimters i.e. ‘,’ , ‘/’ and ‘-‘.
import re inp_str= "Welcome, to this / Pointer - Python" print("Actual String: ",inp_str) # Split the string with multiple delimiters listOfstrs = re.split(',|/|-', inp_str) print(listOfstrs)
Output:
Actual String: Welcome, to this / Pointer - Python ['Welcome', ' to this ', ' Pointer ', ' Python']
We can see that string is split into four strings.
Example 2:
In this example, we have a string that has multiple delimiters – “Welcome, to this / Pointer,c^programming & – Python”. Now we will split this string with multiple delimeters.
import re inp_str= "Welcome, to this / Pointer,c^programming & - Python" print("Actual String: ",inp_str) # Split the string with multiple delimiters listOfstrs = re.split(',|/|-|^|&', inp_str) print(listOfstrs)
Output:
Actual String: Welcome, to this / Pointer,c^programming & - Python ['', 'Welcome', ' to this ', ' Pointer', 'c^programming ', ' ', ' Python']
We can see that string is split into seven strings.
Split a string with multiple delimiters using split() with ‘[]’
To split a string with different delimeters, specify them inside [] separated by a comma and pass as argument to regex’s split() function.
Syntax:
re.split(r'[delimiter1,delimiter2,............]\s*',inp_str)
It takes two parameters.
1. the first parameter takes multiple delimiters separated by ‘,’ inside []
2. the second parameter is the input string.
It splits the given string into multiple substrings based on the specified delimeters and returns those substrings in a list.
Example 1:
In this example, we have a string that has multiple delimiters – “Welcome, to this / Pointer – Python”. Now we will split this string using different delimters.
import re inp_str= "Welcome, to this / Pointer - Python" print("Actual String: ",inp_str) # Split the string with multiple delimiters listOfstrs = re.split(r'[,,/,-]\s*', inp_str) print(listOfstrs)
Output:
Actual String: Welcome, to this / Pointer - Python ['Welcome', 'to this ', 'Pointer ', 'Python']
We can see that string is split into four strings.
Example 2:
In this example, we have a string that has multiple delimiters – “Welcome, to this / Pointer,c^programming &- Python”. Now we will split this string.
import re inp_str= "Welcome, to this / Pointer,c^programming &- Python" print("Actual String: ",inp_str) # Split the string with multiple delimiters listOfstrs = re.split(r'[,/,-,^,&]\s*',inp_str) print(listOfstrs)
Output:
Actual String: Welcome, to this / Pointer,c^programming &- Python ['Welcome', 'to this ', 'Pointer', 'c', 'programming ', '- Python']
We can see that string is split into six strings.
Summary
In this article, we learned how to split strings with single and multiple delimiters using the split() method. Happy Coding.