'How to split a string with whitespace and underscore in Python [closed]

I hope this will find you guys well, can you please show me how to split text with whitespace and underscore? For Example,

txt= "Im Alex_from_canada"

Output should be

['Im','Alex','frpm','canada']

Looking Forward, Stay blessed



Solution 1:[1]

you could try something like :

import re

r = re.compile("\s+|_")
s = "bal1      bla2_bla3"

print(r.split(s)) --->['bal1', 'bla2', 'bla3']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 baskettaz