How we can break a string with multiple delimiters in Python?



We can break a string with multiple delimiters using the re.split(delimiter, str) method. It takes a regex of delimiters and the string we need to split. For example:

a='Beautiful, is; better*than\nugly'
import re
print(re.split('; |, |\*|\n',a))

We get the output

['Beautiful', 'is', 'better', 'than', 'ugly']

Advertisements