用Python打印{1}

用Python打印{1},python,python-3.x,format,python-3.5,Python,Python 3.x,Format,Python 3.5,我想在Python3.5中得到一个打印输出,看起来像{1},但是我没有正确的.format语法。我试过: print('{{}}'.format('1')) # prints {} print('{{id}}'.format(id='1')) # prints {id} print('{{0}}'.format('1')) # prints {0} print('\{{}\}'.format(1)) # ValueError: Single '}' e

我想在Python3.5中得到一个打印输出,看起来像
{1}
,但是我没有正确的
.format
语法。我试过:

print('{{}}'.format('1'))        # prints {}
print('{{id}}'.format(id='1'))   # prints {id}
print('{{0}}'.format('1'))       # prints {0}
print('\{{}\}'.format(1))        # ValueError: Single '}' encountered in format string
打印({}.format())的正确语法是什么?

您可以执行以下操作:

print ('{' + format('1') + '}')
>>> print('{{{}}}'.format(1))
{1}
最里面的
{}
用于插入
1
。最外层的
{{
}
用于打印
{
}
,但您需要其中两个来转义
{
}
(因为它们通常用于指示位置)。

您可以执行以下操作:

>>> print('{{{}}}'.format(1))
{1}
最里面的
{}
用于插入
1
。最外层的
{{
}
用于打印
{
}
,但您需要其中两个来转义
{
}
(因为它们通常用于指示位置)