我尝试使用不完整的kwargs格式化字符串,如下所示:
input is like "{key1} {key2} {key3}"
output will be like "value1 value2 {key3}"
我在下面试试,
>>> "{key1} {key2} {key3}".format(key1='value1', key2='value2')
但出现以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'key3'
了解最佳方法是什么?
答案 0 :(得分:3)
解决此问题的两种方法,
双重逃避模式
>>> "{key1} {key2} {{key3}}".format(key1='value1', key2='value2')
或者将{key3}
作为值
>>> "{key1} {key2} {key3}".format(key1='value1',key2='value2',key3='{key3}')