Q: How to print a dictionary using string format in python When I try to run the following code
a= """{"hi":"hello "} {user}"""
a.format({"user":"xxx"})

I get this error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '"hi"'

Please let me know how to solve this problem, I have been trying for a long time.

A: You need to escape the { and } and then you need to pass keyword arguments to format, not a dict
a= """{{"hi":"hello "}} {user}"""
print a.format(**{"user":"xxx"})
print a.format(user="xxx")

output
{"hi":"hello "} xxx
{"hi":"hello "} xxx

Or if you are trying to get the string representation:
a= """{{"hi":"hello "}} {user}"""
print a.format(user=str({"user":"xxx"}))

output 
{"hi":"hello "} {'user': 'xxx'}

