非工作示例:

print(" \{ Hello \} {0} ".format(42))

所需输出:

 {Hello} 42 

当前回答

你可以通过加倍牙套来逃避。

Eg:

x = "{{ Hello }} {0}"
print(x.format(42))

其他回答

试试看:

x=“{{你好}}{0}”

你可以通过加倍牙套来逃避。

Eg:

x = "{{ Hello }} {0}"
print(x.format(42))

您想用字符{或}格式化字符串

你只需要把它们加倍。

格式{带有f“{{”,}带有f‘}}”

So :

name = "bob"
print(f'Hello {name} ! I want to print }} and {{ or {{ }}')

输出:

你好,鲍勃!我想打印}和{或{}

对于确切的示例,OR:

number = 42
print(f'{{Hello}} {number}')

将打印:

{你好}42

我使用了双{{}}来防止fstring值注入,

例如,这里是我的Postgres UPDATE语句,用于更新整数数组列,该列采用表达式{}来捕获数组,即:

ports=“{100200300}”

使用fstrings,

ports = [1,2,3]

query = f"""
   UPDATE table SET ports = '{{{ports}}}' WHERE id = 1
"""

实际查询语句将是,

UPDATE table SET ports = '{1,2,3}'

这是一个有效的延期

我参加这个聚会迟到了,真是可笑。我成功地将支架放置在替换元件中,如下所示:

print('{0} {1}'.format('{hello}', '{world}'))

打印

{hello} {world}

严格地说,这不是OP所要求的,因为他/她希望格式字符串中包含大括号,但这可能会对某人有所帮助。