【发布时间】:2021-12-17 20:17:40
【问题描述】:
为此,
name='the name'
address='the address'
s = '''{
"name": "{0}",
"address": "{1}"
}
'''
print(s.format(name, address))
我希望它会打印以下内容。
{
"name": "the name",
"address": "the address"
}
但是我得到了以下错误。为什么以及解决方案是什么?
KeyError Traceback (most recent call last)
<ipython-input-8-eb4c74e2dd68> in <module>
8 '''
9
---> 10 print(s.format(name, address))
KeyError: '\n "name"'
【问题讨论】:
-
尝试删除大括号。
-
转义可打印的括号:
s = '''{{....
标签: python string string-formatting