python 中如何删除字符串连续的空格,只保留一个呢?
学习是最好的投资!
Is this okay?
s = "abc def ghi xy" print ' '.join(filter(lambda x: x, s.split(' ')))
' '.join(s.split())
Use regular expressions:
import re re.sub(r'\s+', ' ', 'dfadf dfa ds ')
Last regular rule (it is said that this is considered an abuse of regular rules)
import re re.sub(' +', ' ', s)
Is this okay?
@felix021 in the comment, yes, there is no need to filter out the '' empty string, just connect it directly:Use regular expressions:
Last regular rule (it is said that this is considered an abuse of regular rules)