I have a bunch of strings, some of them have \' rec\'. I want to remove that only if those are the last 4 characters.
\' rec\'
So in other words I have
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