How can i split a String with delimeters being \t and ' '(space)? For example:
string="\t Hello World\t"
newString=['Hello','World']
How can i split a String with delimeters being \t and ' '(space)? For example:
string="\t Hello World\t"
newString=['Hello','World']
Use re.split
with the delimiter [\t ]+
:
string = "\t Hello World\t"
parts = re.split(r'[\t ]+', string.strip())
print(parts)
This prints:
['Hello', 'World']
Note that I strip the leading and trailing whitespace before calling re.split
. Also, if you would accept just splitting on any whitespace, we could have used re.split(r'\s+', string.strip())
instead.