Python split multiple delimiters | Example code

Use the built-in split method to split strings with multiple delimiters in Python. Just pass multiple delimiters with space into a method.

import re
re.split('; |, ',str)

Example Split string with multiple delimiters in Python

Simple example code. Where delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character.

You have to import the re module for this example.

import re

s = 'Beautiful, is; better*than\nugly'

res = re.split('; |, |\*|\n', s)



Python split multiple delimiters

Another example

import re
text = 'The quick brown\nfox jumps*over the lazy dog.'
print(re.split('; |, |\*|\n',text))

Output: [‘The quick brown’, ‘fox jumps’, ‘over the lazy dog.’]

Do comment if you have any doubts and suggestions on this Python split topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.