{0[Jack]:d} in Python .format(),这里的'0'表示什么?

问题描述 投票:0回答:1
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}

print('Jack: {[Jack]:d}'.format(table))  

print('Jack: {0[Jack]:d} {0[Jack]:d}'.format(table))

#print('Jack: {[Jack]:d} {[Jack]:d}'.format(table))  
==> IndexError: tuple index out of range

{0[Jack]:d}
中的 0 表示什么,为什么最后一个打印语句会引发索引错误?

资源:https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings

python format string-formatting
1个回答
0
投票

{0[插孔]:20d}, {0[插孔]:10f}, [0[插孔]:20e}

0 是替代指数。本章最后一个例子更容易理解(7.1.2. String format() 方法)。在这种情况下,只能更改一项。所以,指数是0.

d 是数字,代表整数; f 为浮点数,小数点后 6 位; e 为科学记数法,1.2e−4=0.00012

20, or 10... : 20 (or 10) characters string length in your output.

© www.soinside.com 2019 - 2024. All rights reserved.