Is there a simple way to remove multiple spaces in a string?

后端 未结 29 1429
星月不相逢
星月不相逢 2020-11-22 08:17

Suppose this string:

The   fox jumped   over    the log.

Turning into:



        
29条回答
  •  伪装坚强ぢ
    2020-11-22 09:12

    foo is your string:

    " ".join(foo.split())
    

    Be warned though this removes "all whitespace characters (space, tab, newline, return, formfeed)" (thanks to hhsaffar, see comments). I.e., "this is \t a test\n" will effectively end up as "this is a test".

提交回复
热议问题