Use str.split([sep[, maxsplit]])
with no sep
or sep=None
:
From docs:
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.
Demo:
>>> myString.split()
['I', 'want', 'to', 'Remove', 'all', 'white', 'spaces,', 'new', 'lines', 'and', 'tabs']
Use str.join
on the returned list to get this output:
>>> ' '.join(myString.split())
'I want to Remove all white spaces, new lines and tabs'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…