非工作示例:
print(" \{ Hello \} {0} ".format(42))
所需输出:
{Hello} 42
非工作示例:
print(" \{ Hello \} {0} ".format(42))
所需输出:
{Hello} 42
当前回答
你可以通过加倍牙套来逃避。
Eg:
x = "{{ Hello }} {0}"
print(x.format(42))
其他回答
试试看:
x=“{{你好}}{0}”
我在尝试打印文本时遇到了这个问题,我可以将文本复制粘贴到Latex文档中。我扩展了这个答案,并使用了命名替换字段:
假设您要打印多个变量的乘积,其中包含以下索引,在乳胶中为$A_{0042}*A_{3141}*A_{2718}*A_{0042}$以下代码使用命名字段执行此项工作,以便对许多索引保持可读:
idx_mapping = {'i1':42, 'i2':3141, 'i3':2178 }
print('$A_{{ {i1:04d} }} * A_{{ {i2:04d} }} * A_{{ {i3:04d} }} * A_{{ {i1:04d} }}$'.format(**idx_mapping))
key = "FOOBAR"
print(f"hello {{{key}}}")
输出
hello {FOOBAR}
万一有人想用fstrings在大括号内打印东西。
你可以通过加倍牙套来逃避。
Eg:
x = "{{ Hello }} {0}"
print(x.format(42))
尝试这样做:
x = " {{ Hello }} {0} "
print x.format(42)