在Python教程(https://docs.python.org/3.8/tutorial/inputoutput.html)中,他们使用了{: -9 },我不知道-9是做什么的?:
yes_votes = 42_572_654
no_votes = 43_132_495
percentage = yes_votes / (yes_votes + no_votes)
'{:-9} YES votes {:2.2%}'.format(yes_votes, percentage)产出: 42572654票赞成49.67%
发布于 2020-09-09 08:03:12
:-9值表示填充。如果删除-,效果将是相同的。
但是,根据文档,-表示一个符号应该只用于负数(这是默认行为)。
示例:
Replacing %+f, %-f, and % f and specifying a sign
>>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always
'+3.140000; -3.140000'
>>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers
' 3.140000; -3.140000'
>>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}'
'3.140000; -3.140000'有关Python格式规范的更多信息,可以找到这里
https://stackoverflow.com/questions/63806417
复制相似问题