非工作示例:
print(" \{ Hello \} {0} ".format(42))
所需输出:
{Hello} 42
非工作示例:
print(" \{ Hello \} {0} ".format(42))
所需输出:
{Hello} 42
当前回答
如果要仅打印大括号的一侧:
a=3
print(f'{"{"}{a}')
>>> {3
其他回答
或者只是参数化括号本身?可能很冗长。
x = '{open_bracket}42{close_bracket}'.format(open_bracket='{', close_bracket='}')
print(x)
# {42}
您可以使用“引号墙”将格式化字符串部分与常规字符串部分分开。
发件人:
print(f"{Hello} {42}")
to
print("{Hello}"f" {42}")
一个更清楚的例子是
string = 10
print(f"{string} {word}")
输出:
NameError: name 'word' is not defined
现在,添加报价墙,如下所示:
string = 10
print(f"{string}"" {word}")
输出:
10 {word}
key = "FOOBAR"
print(f"hello {{{key}}}")
输出
hello {FOOBAR}
万一有人想用fstrings在大括号内打印东西。
你可以通过加倍牙套来逃避。
Eg:
x = "{{ Hello }} {0}"
print(x.format(42))
尝试这样做:
x = " {{ Hello }} {0} "
print x.format(42)