
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Split Strings on Multiple Delimiters in Python
Depending on the flexibility and complexity required, we can split a string on multiple delimiters in several ways such as using Python's re.split() method, which is a combination of various string methods. Some of the commonly used approaches to split strings on multiple delimiters.
-
str.replace() and str.split()
-
re.split() Method
-
translate() and maketrans() Methods
Using 'str.replace()' and 'str.split()'
This method involves replacing all delimiters in the string with a common one such as (a space or a comma), use the str.replace() to replace each delimiter with a common character. After replacing the delimiters use the str.split() method to split the string.
Example
In the following example, each replace() call replaces a delimiter with a space. After all the delimiters have been replaced with a space, str.split() splits the string into a list.
text = "apple,orange;banana|grape:pear" # Replace each delimiter with a space text = text.replace(",", " ").replace(";", " ").replace("|", " ").replace(":", " ") # Split the string on spaces result = text.split() print(result)
Output
['apple', 'orange', 'banana', 'grape', 'pear']
Using 're.split()' Method
Python provides the re (Regular Expressions) module. Specifically, the re. split() function is an effective tool that allows us to split a string using multiple delimiters. Regex or Regular expressions are sequences of characters that define a search pattern.
Example
In the below example code, we used the re.split() method to split the string at every occurrence of a semicolon (;), comma (,), or space ( ). The | symbol is used in regular expressions to mean "or", so |,| can be read as a "semicolon or comma or space".
import re text = "Python;is,a powerful:language" words = re.split(';|,| ', text) print(words)
Output
['Python', 'is', 'a', 'powerful:language']
Using 'translate()' and 'maketrans()' Methods
In Python, for character mapping and replacement these two maketrans() and translate() methods are used which are provided by the str class. Using both methods in combination gives an efficient way to replace multiple delimiters.
Example
In the below example code, the maketrans() method returns a translation table that maps semicolons (;) and colons (:) to commas (,). The translate() method replaced specific characters. Later, we can use str.split(',') to split the text into words and print the extracted words.
text = "Python;is,a powerful:language" # Create a translation table mapping ';' and ':' to ',' table = text.maketrans(";:", ",,") # Apply the translation table text = text.translate(table) # Now we can split on the comma words = text.split(',') print(words)
Output
['Python', 'is', 'a powerful', 'language']