Split a string with multiple delimiters in Python

Introduction :

Python built in split method of the re module can be used to split one string based on a regular expression. Regular expression can be used to split one string using multiple delimiters. In this quick tutorial, I will show you how to split a string with multiple delimiters in Python.

Python example program :

We will split one string on numbers. For example :

The12quick02brown44fox99jumps90over7the9lazy000dog

For this string input, it will give the below output :

['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

The below function will do that :

import re

given_str = 'The12quick02brown44fox99jumps90over7the9lazy000dog'
print(re.split('\d+',given_str))

The above program will print the below output :

['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

You can use any other regex pattern if you want.

Similar tutorials :