PHPz2017-04-17 11:14:27
Is this okay?
s = "abc def ghi xy" print ' '.join(filter(lambda x: x, s.split(' ')))@felix021 in the comment, yes, there is no need to filter out the '' empty string, just connect it directly:
' '.join(s.split())
大家讲道理2017-04-17 11:14:27
Use regular expressions:
import re re.sub(r'\s+', ' ', 'dfadf dfa ds ')
天蓬老师2017-04-17 11:14:27
Last regular rule (it is said that this is considered an abuse of regular rules)
import re re.sub(' +', ' ', s)