Remove substring only at the end of string

后端 未结 11 725
再見小時候
再見小時候 2020-12-23 13:23

I have a bunch of strings, some of them have \' rec\'. I want to remove that only if those are the last 4 characters.

So in other words I have



        
11条回答
  •  感动是毒
    2020-12-23 13:37

    Here is a one-liner version of Jack Kelly's answer along with its sibling:

    def rchop(s, sub):
        return s[:-len(sub)] if s.endswith(sub) else s
    
    def lchop(s, sub):
        return s[len(sub):] if s.startswith(sub) else s
    

提交回复
热议问题