Replace all quotes in a string with escaped quotes?

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"'

Your second attempt is correct, but you're getting confused by the difference between the repr and the str of a string. A more idiomatic way of doing your second way is to use "raw strings":

>>> s = 'This sentence has some "quotes" in it\n'
>>> print s
This sentence has some "quotes" in it

>>> print s.replace('"', r'\"')  # raw string used here
This sentence has some \"quotes\" in it

>>> s.replace('"', r'\"')
'This sentence has some \\"quotes\\" in it\n'

Raw strings are WYSIWYG: backslashes in a raw string are just another character. It is - as you've discovered - easy to get confused otherwise ;-)

Printing the string (the 2nd-last output above) shows that it contains the characters you want now.

Without print (the last output above), Python implicitly applies repr() to the value before displaying it. The result is a string that would produce the original if Python were to evaluate it. That's why the backlashes are doubled in the last line. They're not in the string, but are needed so that if Python were to evaluate it each \\ would become one \ in the result.


Your last attempt was working as you expected it to. The double backslashes you see are simply a way of displaying the single backslashes that are actually in the string. You can verify this by checking the length of the result with len().

For details on the double backslash thing, see: __repr__()


UPDATE:

In response to your edited question, how about one of these?

print repr(s).replace('"', '\\"')
print s.encode('string-escape').replace('"', '\\"')

Or for python 3:

print(s.encode('unicode-escape').replace(b'"', b'\\"'))