Remove all whitespace in a string

后端 未结 11 1671
一整个雨季
一整个雨季 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
    0 讨论(0)
  • 2020-11-22 04:21

    In addition, strip has some variations:

    Remove spaces in the BEGINNING and END of a string:

    sentence= sentence.strip()
    

    Remove spaces in the BEGINNING of a string:

    sentence = sentence.lstrip()
    

    Remove spaces in the END of a string:

    sentence= sentence.rstrip()
    

    All three string functions strip lstrip, and rstrip can take parameters of the string to strip, with the default being all white space. This can be helpful when you are working with something particular, for example, you could remove only spaces but not newlines:

    " 1. Step 1\n".strip(" ")
    

    Or you could remove extra commas when reading in a string list:

    "1,2,3,".strip(",")
    
    0 讨论(0)
  • 2020-11-22 04:28

    An alternative is to use regular expressions and match these strange white-space characters too. Here are some examples:

    Remove ALL spaces in a string, even between words:

    import re
    sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE)
    

    Remove spaces in the BEGINNING of a string:

    import re
    sentence = re.sub(r"^\s+", "", sentence, flags=re.UNICODE)
    

    Remove spaces in the END of a string:

    import re
    sentence = re.sub(r"\s+$", "", sentence, flags=re.UNICODE)
    

    Remove spaces both in the BEGINNING and in the END of a string:

    import re
    sentence = re.sub("^\s+|\s+$", "", sentence, flags=re.UNICODE)
    

    Remove ONLY DUPLICATE spaces:

    import re
    sentence = " ".join(re.split("\s+", sentence, flags=re.UNICODE))
    

    (All examples work in both Python 2 and Python 3)

    0 讨论(0)
  • 2020-11-22 04:30

    try this.. instead of using re i think using split with strip is much better

    def my_handle(self):
        sentence = ' hello  apple  '
        ' '.join(x.strip() for x in sentence.split())
    #hello apple
        ''.join(x.strip() for x in sentence.split())
    #helloapple
    
    0 讨论(0)
  • 2020-11-22 04:35

    Be careful:

    strip does a rstrip and lstrip (removes leading and trailing spaces, tabs, returns and form feeds, but it does not remove them in the middle of the string).

    If you only replace spaces and tabs you can end up with hidden CRLFs that appear to match what you are looking for, but are not the same.

    0 讨论(0)
提交回复
热议问题