x = \" \\{ Hello \\} {0} \"
print(x.format(42))
gives me : Key Error: Hello\\\\
I want to print the output: {Hello} 42>
The OP wrote this comment:
I was trying to format a small JSON for some purposes, like this:
'{"all": false, "selected": "{}"}'.format(data)
to get something like{"all": false, "selected": "1,2"}
It's pretty common that the "escaping braces" issue comes up when dealing with JSON.
I suggest doing this:
import json
data = "1,2"
mydict = {"all": "false", "selected": data}
json.dumps(mydict)
It's cleaner than the alternative, which is:
'{{"all": false, "selected": "{}"}}'.format(data)
Using the json
library is definitely preferable when the JSON string gets more complicated than the example.