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
You could use a regular expression as well:
from re import sub str = r"this is some string rec" regex = r"(.*)\srec$" print sub(regex, r"\1", str)