为什么所有Python格式的示例中都有一个:符号?

2024-05-14 06:07:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在看一个具体的例子:

x = 3.45678
print({':.2f'}.format(x))

我一辈子都找不到任何关于结肠的文件。 http://docs.python.org/2/library/string.html#grammar-token-precision

我真的希望有人能指出我在哪里可以自己学会的。在


Tags: 文件orgtokenformathttpdocsstringhtml
2条回答

:只是从格式中快速表达索引

它有时是隐式的(Python2.7+)

"{:.2f}  {:d}".format(0.0,1)  == "{0:.2f} , {1:d}".format(0.0,1)
#first   #second
#you could do
"{0:0.2f} {0:0.3f} {1:d}".format(0.0,1)
#or
"{apples:d} {oranges:d}".format(apples=5,oranges=7)

它在您提到的文档中,但在Format String Syntax下。在

The field_name is optionally followed by a conversion field, which is preceded by an exclamation point '!', and a format_spec, which is preceded by a colon ':'. These specify a non-default format for the replacement value.

相关问题 更多 >