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
Taking inspiration from @David Foster's answer, I would do
def _remove_suffix(text, suffix): if text is not None and suffix is not None: return text[:-len(suffix)] if text.endswith(suffix) else text else: return text
Reference: Python string slicing