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
As kind of one liner generator joined:
test = """somestring='this is some string rec'
this is some string in the end word rec
This has not the word."""
match = 'rec'
print('\n'.join((line[:-len(match)] if line.endswith(match) else line)
for line in test.splitlines()))
""" Output:
somestring='this is some string rec'
this is some string in the end word
This has not the word.
"""