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

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

Suppose this string:

The   fox jumped   over    the log.

Turning into:



        
29条回答
  •  一向
    一向 (楼主)
    2020-11-22 09:02

    import re
    s = "The   fox jumped   over    the log."
    re.sub("\s\s+" , " ", s)
    

    or

    re.sub("\s\s+", " ", s)
    

    since the space before comma is listed as a pet peeve in PEP 8, as mentioned by user Martin Thoma in the comments.

提交回复
热议问题