Remove all whitespace in a string

后端 未结 11 1795
一整个雨季
一整个雨季 2020-11-22 04:02

I want to eliminate all the whitespace from a string, on both ends, and in between words.

I have this Python code:

def my_handle(self):
    sentence          


        
11条回答
  •  长发绾君心
    2020-11-22 04:12

    import re    
    sentence = ' hello  apple'
    re.sub(' ','',sentence) #helloworld (remove all spaces)
    re.sub('  ',' ',sentence) #hello world (remove double spaces)
    

提交回复
热议问题