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

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

Suppose this string:

The   fox jumped   over    the log.

Turning into:



        
29条回答
  •  攒了一身酷
    2020-11-22 09:05

    Solution for Python developers:

    import re
    
    text1 = 'Python      Exercises    Are   Challenging Exercises'
    print("Original string: ", text1)
    print("Without extra spaces: ", re.sub(' +', ' ', text1))
    

    Output:
    Original string: Python Exercises Are Challenging Exercises Without extra spaces: Python Exercises Are Challenging Exercises

提交回复
热议问题