繁体   English   中英

Python格式化的文字字符串和千位分隔符

[英]Python formatted literal string and thousand separator

有没有一种方法可以将python3.6格式的文字字符串( PEP498 )与格式规范结合在一起?

print('Count: {:,}'.format(count)) # Count: 10,000

print(f'Count: {count}') # Count: 10000

我能想到的最接近的东西是使用像这样的format函数:
print(f'Count: {format(count, "6,d")}') # Count: 10,000 # can use "6,d" or any other format that puts thousands separators

这是最好的方法吗?

您给链接

F字符串使用与str.format相同的格式说明符迷你语言。 str.format()类似,可选的格式说明符可以包含在f字符串中,并用冒号与表达式(或类型转换,如果指定)分开。 如果未提供格式说明符,则使用空字符串。

至于你的例子:

>>> count = 10000
>>> print('Count: {:,}'.format(count))
Count: 10,000
>>> print(f'Count: {count:,}')
Count: 10,000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM