Using the strip() Function to Remove Double Quotes from String in Python. We use the strip() function in Python to delete characters from the start or end of the string. We can use this method to remove the quotes if they exist at the start or end of the string.
if you want to escape double quote in JSON use \\ to escape it.
You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.
You should be using the json
module. json.dumps(string)
. It can also serialize other python data types.
import json
>>> s = 'my string with "double quotes" blablabla'
>>> json.dumps(s)
<<< '"my string with \\"double quotes\\" blablabla"'
Note that you can escape a json array / dictionary by doing json.dumps twice and json.loads twice:
>>> a = {'x':1}
>>> b = json.dumps(json.dumps(a))
>>> b
'"{\\"x\\": 1}"'
>>> json.loads(json.loads(b))
{u'x': 1}
>>> s = 'my string with \\"double quotes\\" blablabla'
>>> s
'my string with \\"double quotes\\" blablabla'
>>> print s
my string with \"double quotes\" blablabla
>>>
When you just ask for 's' it escapes the \ for you, when you print it, you see the string a more 'raw' state. So now...
>>> s = """my string with "double quotes" blablabla"""
'my string with "double quotes" blablabla'
>>> print s.replace('"', '\\"')
my string with \"double quotes\" blablabla
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With