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 :

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊❤️❤️