Python Remove last 3 characters of a string

前端 未结 10 1199
-上瘾入骨i
-上瘾入骨i 2021-01-29 23:31

I\'m trying to remove the last 3 characters from a string in python, I don\'t know what these characters are so I can\'t use rstrip, I also need to remove any white

10条回答
  •  鱼传尺愫
    2021-01-30 00:22

    You might have misunderstood rstrip slightly, it strips not a string but any character in the string you specify.

    Like this:

    >>> text = "xxxxcbaabc"
    >>> text.rstrip("abc")
    'xxxx'
    

    So instead, just use

    text = text[:-3] 
    

    (after replacing whitespace with nothing)

提交回复
热议问题