[英]Python formatted literal string and thousand separator
Is there a way to combine python3.6 formatted literal strings ( PEP498 ) with format specifications ? 有没有一种方法可以将python3.6格式的文字字符串( PEP498 )与格式规范结合在一起?
print('Count: {:,}'.format(count)) # Count: 10,000
print(f'Count: {count}') # Count: 10000
The closest thing I can think of is using the format
function like this: 我能想到的最接近的东西是使用像这样的
format
函数:
print(f'Count: {format(count, "6,d")}') # Count: 10,000 # can use "6,d" or any other format that puts thousands separators
Is this the best way to do that? 这是最好的方法吗?
from the link you gave : 从您给的链接 :
F-strings use the same format specifier mini-language as str.format.
F字符串使用与str.format相同的格式说明符迷你语言。 Similar to str.format() , optional format specifiers maybe be included inside the f-string, separated from the expression (or the type conversion, if specified) by a colon.
与str.format()类似,可选的格式说明符可以包含在f字符串中,并用冒号与表达式(或类型转换,如果指定)分开。 If a format specifier is not provided, an empty string is used.
如果未提供格式说明符,则使用空字符串。
as for your example: 至于你的例子:
>>> count = 10000
>>> print('Count: {:,}'.format(count))
Count: 10,000
>>> print(f'Count: {count:,}')
Count: 10,000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.