安德里亚·贝尔贡佐
有没有办法将 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-string 中,用冒号与表达式(或类型转换,如果指定)分开。如果未提供格式说明符,则使用空字符串。
至于你的例子:
>>> count = 10000
>>> print('Count: {:,}'.format(count))
Count: 10,000
>>> print(f'Count: {count:,}')
Count: 10,000
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
编辑于
我来说两句