-1

How can i split a String with delimeters being \t and ' '(space)? For example:

string="\t Hello   World\t"
newString=['Hello','World']
George
  • 15
  • 4
  • 1
    Possible duplicate of [Split Strings into words with multiple word boundary delimiters](https://stackoverflow.com/questions/1059559/split-strings-into-words-with-multiple-word-boundary-delimiters) – A.Comer Nov 21 '19 at 17:28

1 Answers1

2

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360