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

PythonServer Side ProgrammingProgramming

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']
raja
Published on 12-Dec-2017 17:08:36
Advertisements