簡體   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