Hi usually when working with Javascript I use the json module provided by Python. It will escape the string as well as a bunch of other things as user2357112 has pointed out.
import json
string = 'This sentence has some "quotes" in it\n'
json.dumps(string) #gives you '"This sentence has some \\"quotes\\" in it\\n"'