我是 python 的新手,最近我已经迁移到 Python 3 并且
我正在努力适应 format()
函数。
我想做的是 print()
我创造的温度
在 float 中,像这样:
temperature = [23, 24, 25, 26, 27]
print("Today's temperature is %.02f" % (temperature[0]))
而不是使用 %.02f
,我想知道如何
可以改为在 format()
函数中编写
%
百分比。
最佳答案
你必须使用{:.02f}
>>> temperature = [23, 24, 25, 26, 27]
>>> print("Today's temperature is %.02f" % (temperature[0]))
Today's temperature is 23.00
>>> print("Today's temperature is {:.02f}".format(temperature[0]))
Today's temperature is 23.00
更多信息可以在 documentation 中找到.但是请参阅 Format Examples
'%03.2f'
can be translated to'{:03.2f}'
关于python - 如何在Python格式函数中应用 "%.02f"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32659685/