x = " { Hello } {0} " print x.format(42)
gives me : Key Error: Hello\\
Key Error: Hello\\
(给我: Key Error: Hello\\)
I want to print the output: {Hello} 42
{Hello} 42
(我要打印输出: {Hello} 42)
You need to double the {{ and }} :
{{
}}
(您需要将{{和}}加倍:)
>>> x = " {{ Hello }} {0} " >>> print x.format(42) ' { Hello } 42 '
Here's the relevant part of the Python documentation for format string syntax :
(这是Python文档中有关格式字符串语法的相关部分:)
Format strings contain “replacement fields” surrounded by curly braces {} . (格式字符串包含用花括号{}括起来的“替换字段”。)Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. (花括号中不包含的所有内容均视为文字文本,该文本原样复制到输出中。)If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} . (如果需要在文字文本中包含大括号字符,可以通过加倍{{和}}来转义。)
Format strings contain “replacement fields” surrounded by curly braces {} .
{}
(格式字符串包含用花括号{}括起来的“替换字段”。)
(花括号中不包含的所有内容均视为文字文本,该文本原样复制到输出中。)
(如果需要在文字文本中包含大括号字符,可以通过加倍{{和}}来转义。)
1.4m articles
1.4m replys
5 comments
57.0k users