import re
print(re.sub('([\\"])', '\\\\\\1', 'it\'s "this"'))
print(re.sub("([\\'])", '\\\\\\1', 'it\'s "this"'))
print(re.sub('([\\" \\\'])', '\\\\\\1', 'it\'s "this"'))
Python escape regex special characters