Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要使用 format() 方法打印出包含 ${} 的字符串
例如,我想打印出来
"hello ${a} hello"
使用这个 python 代码
print "{string1} ${a} {string1}".format(string1="hello")
但是,我得到 KeyError'a',因为 format() 需要 {a} 的输入字符串。如何教 format() 忽略 ${a}?
您需要将 { 和 } 加倍到 {{ 到 }}:
print "{string1} ${{a}} {string1}".format(string1="hello")