Remove all whitespace in a string

后端 未结 11 1648
一整个雨季
一整个雨季 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:18

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

    >>> import re
    >>> re.sub("\s+", # one or more repetition of whitespace
        '', # replace with empty string (->remove)
        ''' hello
    ...    apple
    ... ''')
    'helloapple'
    
    • https://en.wikipedia.org/wiki/Whitespace_character

    Python docs:

    • https://docs.python.org/library/stdtypes.html#textseq
    • https://docs.python.org/library/stdtypes.html#str.replace
    • https://docs.python.org/library/string.html#string.replace
    • https://docs.python.org/library/re.html#re.sub
    • https://docs.python.org/library/re.html#regular-expression-syntax

提交回复
热议问题