Python split multiple delimiters
In this post, you will learn how to split multiple delimiters using the Python programming language. During programming, you may face some situations where you need to utilise the split method in Python to split a complete string into separate words, not to part with just one character but rather with different characters.
A delimiter is a sequence of one or more characters for specifying the boundary between separate, independent regions in plain text, mathematical expressions, or other data streams.
A delimiter is an arrangement of one or more characters used to indicate the limit between separate, independent regions in plain content, mathematics, or other information streams. Some examples of delimiters are commas, braces, brackets, etc. Delimiters address one of several different means for determining limits in an information stream.
Python string.split() method
The split() method of Python splits a string into a list. Here is the syntax of the split() method.
string.split(separator, maxsplit)
Here, the separator specifies the separator to use when splitting the string. By default, whitespace is a separator. The maxsplit is a number that specifies how many times the string will be split into a maximum of the provided number of times. Both the separator and the maxsplit are optional parameters.
Python string.split() Example
Here is the basic example of splitting a string using a delimiter in Python program. If the argument is omitted, it will be separated by whitespace. Consecutive whitespace includes spaces, newlines \n and tabs \t, and consecutive whitespace is handled together.
str = 'best,platform,to,learn,programming'
str = str.split(',')
print(str)
str1 = 'Success;is;journey'
str1 = str1.split(';')
print(str1)
Output of the above code -
['best', 'platform', 'to', 'learn', 'programming']
['Success', 'is', 'journey']
Python string.split() Multiple Delimiters Example
The string.split() method for string objects is truly implied for basic cases, and doesn't consider multiple delimiters. To achieve this, we can use the re (regular expression) module of Python. This module is used for describing a search pattern or extracting information from text. It specifies the regular expression pattern in the first parameter and the target character string in the second parameter. Here is the basic example of splitting a string with multiple delimiters.
import re
str = 'best; platform, to, learn ! programming'
str = re.split(r', |;|!',str)
print(str)
str1 = 'Success; is; journey'
str1 = re.split(r'; |, |\*',str1)
print(str1)
Output of the above code -
['best', ' platform', 'to', 'learn ', ' programming']
['Success', 'is', 'journey']
When utilizing re.split(), you need to be somewhat careful in selecting the regular expression patterns.
Related Articles
Python multiline string
Count consonants in a string Python
Remove character from string Python
Python split string by comma
Python heapq module
range and xrange in Python
2d arrays in Python
splitlines in python
Simple calculator python
strip function in Python
casefold in Python
Prime factors of a number in Python
Python nonlocal keyword
Greatest common divisor Python recursive
Python String isalpha() Method
Program to print ASCII Value of a character
Python program to sort words in alphabetical order
*args and **kwargs in Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module