简体   繁体   English

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

[英]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.

相关问题 python 带空格的格式字符串千位分隔符 - python format string thousand separator with spaces 千位分隔符格式字符串与浮点数 - Thousand separator in format string with floats Python 3.6+ 中是否有格式化的字节字符串文字? - Is there a formatted byte string literal in Python 3.6+? 字符串类型变量的自定义千位分隔符 - Customized thousand separator for string type variable Python用逗号千位分隔符替换中间数字 - Python replace middle digits with commas thousand separator 在python2.7中使用DOT作为千位分隔符 - Using DOT as thousand separator in python2.7 如何在 Excel 中的 Python 或 VBA 中替换千位分隔符并保留其他逗号 - How to replace the thousand separator and keep other commas in Python or VBA in Excel 在 Python pandas DataFrame 中为数字添加千位分隔符的简单方法 - Easy way to add thousand separator to numbers in Python pandas DataFrame 如何在python中使用正则表达式捕获带有千位和小数分隔符的价格 - How do I capture a price with thousand and decimal separator with regex in python Python 控制台日志记录和千位分隔符,同时保留结构化日志兼容性 - Python console logging and thousand separator while retaining structured logs compatibility
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM