How to strip all whitespace from string

前端 未结 11 1872
暖寄归人
暖寄归人 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条回答
  •  -上瘾入骨i
    2020-11-28 19:04

    Remove the Starting Spaces in Python

    string1="    This is Test String to strip leading space"
    print string1
    print string1.lstrip()
    

    Remove the Trailing or End Spaces in Python

    string2="This is Test String to strip trailing space     "
    print string2
    print string2.rstrip()
    

    Remove the whiteSpaces from Beginning and end of the string in Python

    string3="    This is Test String to strip leading and trailing space      "
    print string3
    print string3.strip()
    

    Remove all the spaces in python

    string4="   This is Test String to test all the spaces        "
    print string4
    print string4.replace(" ", "")
    

提交回复
热议问题