Python split multiple delimiters
In this post, you will learn how to split multiple delimiters using Python. During programming, you may face some situations where you need to utilize the split method in Python to split a complete string into separate words, not to part on just one character but rather 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 for indicating the limit between separate, independent regions in plain content, mathematics or other information streams. Some examples of delimiters are comma, braces, brackets, etc. Delimiters address one of different means for determining limits in an information stream.
The split() method splits a string into a list. Here is the syntax of split() method -
Here, the separator specifies the separator to use when splitting the string. By default whitespace is a separator. The maxsplit is a number which specifies to split the string into a maximum of provided number of times. Both separator and maxsplit are optional parameters.
Python string.split() Example
Here is the basic example to split a string using a delimiter -
Output of the above code -
str = 'best,platform,to,learn,programming' str = str.split(',') print(str) str1 = 'Success;is;journey' str1 = str1.split(';') print(str1)
['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 need to use re (regular expression) module. This module is used for describing a search pattern or for extracting information from text. Here is the basic example to split a string with multiple delimiters -
Output of the above code -
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)
['best', ' platform', 'to', 'learn ', ' programming'] ['Success', 'is', 'journey']
When utilizing re.split(), you need to be somewhat careful in selecting the regular expression patterns.
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