I have a python variable containing the string A
and another one containing the string B
.
I want to generate the string which represents the dictionary containing the aforementioned two strings as a key value pair. How can I do it? Using nested curly brackets inside a string literal seems not to work. But I need that notation to use .format()
>>> x = "A"
>>> y = "B"
>>> assert(eval('\{"{}": "{}"\}'.format(x, y)) == str({x: y}))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: '"{}"