Python格式的文本字符串和

2024-03-29 06:30:23 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法将python3.6格式的文本字符串(PEP498)与format specifications结合起来

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

这是最好的方法吗


Tags: or方法函数字符串文本formatuse格式
1条回答
网友
1楼 · 发布于 2024-03-29 06:30:23

link you gave

F-strings use the same format specifier mini-language as 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. If a format specifier is not provided, an empty string is used.

以你为例:

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

相关问题 更多 >