How to strip all whitespace from string

前端 未结 11 1915
暖寄归人
暖寄归人 2020-11-28 18:25

How do I strip all the spaces in a python string? For example, I want a string like strip my spaces to be turned into stripmyspaces, but I cannot s

11条回答
  •  误落风尘
    2020-11-28 19:04

    The simplest is to use replace:

    "foo bar\t".replace(" ", "").replace("\t", "")
    

    Alternatively, use a regular expression:

    import re
    re.sub(r"\s", "", "foo bar\t")
    

提交回复
热议问题