python大括号

您所在的位置:网站首页 python里的冒号怎么打 python大括号

python大括号

2023-08-03 22:32| 来源: 网络整理| 查看: 265

如何在python字符串中打印文字大括号字符并在其上使用.format?

x = " \{ Hello \} {0} "

print x.format(42)

给我:{Hello} 42

我想打印输出:{Hello} 42

10个解决方案

1382 votes

你需要加倍{}和{ {:

>>> x = " { { Hello }} {0} "

>>> print x.format(42)

' { Hello } 42 '

以下是格式字符串语法的Python文档的相关部分:

格式字符串包含由大括号{}包围的“替换字段”。大括号中未包含的任何内容都被视为文本文本,它将不加改变地复制到输出中。 如果您需要在文字文本中包含大括号字符,则可以通过加倍来转义它:{ {和}}。

Greg Hewgill answered 2018-12-17T04:59:05Z

55 votes

你通过加倍括号来逃避它。

例如:

x = "{ { Hello }} {0}"

print x.format(42)

Kamil Kisiel answered 2018-12-17T04:59:29Z

24 votes

试着这样做:

x = " { { Hello }} {0} "

print x.format(42)

DNR answered 2018-12-17T04:59:48Z

21 votes

试试这个:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3