How can I split by 1 or more occurrences of a delimiter in Python? How can I split by 1 or more occurrences of a delimiter in Python? python python

How can I split by 1 or more occurrences of a delimiter in Python?


Just do not give any delimeter?

>>> a="test                            result">>> a.split()['test', 'result']


>>> import re>>> a="test                            result">>> re.split(" +",a)['test', 'result']>>> a.split()['test', 'result']


Just this should work:

a.split()

Example:

>>> 'a      b'.split(' ')['a', '', '', '', '', '', 'b']>>> 'a      b'.split()['a', 'b']

From the documentation:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].