How to split string into words that do not contain

2020-04-08 13:00发布

问题:

My string is:

"This     is a      string"

I want to turn it into a list:

["This", "is", "a", "string"]

I use the split(" ") method, but it adds whitespaces as list elements. Please help,

Best Regards

回答1:

>>> v="This is a  string"

>>> v.split()
['This', 'is', 'a', 'string']

just use split().



回答2:

It won't add whitespace as elements if you just use .split(), instead of .split(' ')

>>> "This     is a     string".split()
['This', 'is', 'a', 'string']


回答3:

Like the docs say, don't pass an argument.

>>> "This is a string".split()
['This', 'is', 'a', 'string']