如何用python replace字符串中间多余的空格转换为一个空格

如题所述

定义一个函数
def replaceAll(old, new, str): while str.find(old) > -1: str = str.replace(old, new) return str

然后调用
context = "hello world nice ok done"print replaceAll(' ', ' ', context)

结果:
hello world nice ok done

要注意old和new不能相同,否则...请自己加个判断
温馨提示:答案为网友推荐,仅供参考
相似回答