How to split a string into words with multiple delimiters in Python

Splitting a string into words with multiple delimiters results in a list with each word in a string, ignoring punctuation marks. For example, splitting “Hello, darkness – my old friend. I’ve come to talk with you again.” into words results in [‘Hello’, ‘darkness’, ‘my’, ‘old’, ‘friend’, “I’ve”, ‘come’, ‘to’, ‘talk’, ‘with’, ‘you’, ‘again’].

Solution for How to split a string into words with multiple delimiters in Python : You can use re.findall() to split a string into words Use re.findall(pattern, string) with pattern as r”[\w’]+” to split a string into words. r”[\w’]+” specifies the desired pattern as groups of consecutive letters and/or apostrophes.

Further reading

Regular expressions (regex) provide a concise way to specify rules for a set of possible strings. You can read more about regular expressions here.


how-to-split-a-string-into-words-with-multiple-delimiters-in-python