import re
string = """This ain't funny, you know.
But this escaped one (\") won't change."""
rx = re.compile(r'''(?<!\\)(["'])''')
string = rx.sub(r'\\\1', string)
print(string)
aW1wb3J0IHJlCgpzdHJpbmcgPSAiIiJUaGlzIGFpbid0IGZ1bm55LCB5b3Uga25vdy4KQnV0IHRoaXMgZXNjYXBlZCBvbmUgKFwiKSB3b24ndCBjaGFuZ2UuIiIiCgpyeCA9IHJlLmNvbXBpbGUocicnJyg/PCFcXCkoWyInXSknJycpCgpzdHJpbmcgPSByeC5zdWIocidcXFwxJywgc3RyaW5nKQpwcmludChzdHJpbmcp