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

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

Suppose this string:

The   fox jumped   over    the log.

Turning into:



        
29条回答
  •  借酒劲吻你
    2020-11-22 09:06

    Another alternative:

    >>> import re
    >>> str = 'this is a            string with    multiple spaces and    tabs'
    >>> str = re.sub('[ \t]+' , ' ', str)
    >>> print str
    this is a string with multiple spaces and tabs
    

提交回复
热议问题