python逗號分隔符
什么是質數? (What is a prime number?)
Many times, while writing the code we need to print the large number separated i.e. thousands separators with commas.
很多時候,在編寫代碼時,我們需要打印大量的分隔符,即用逗號分隔數千個分隔符。
In python, such formatting is easy. Consider the below syntax to format a number with commas (thousands separators).
在python中,這種格式很容易。 考慮以下語法,以逗號分隔數字(千位分隔符) 。
"{:,}".format(n)
Here, n is the number to be formatted.
Given a number n, we have to print it with commas as thousands separators.
給定數字n ,我們必須用逗號將其打印為數千個分隔符。
Example:
例:
Input:
n = 1234567890
Output:
1,234,567,890
Python程序以逗號分隔的形式將數字打印為Python中的數千個分隔符 (Python program to print number with commas as thousands separators in Python)
# function to return number with thousand separator
def formattedNumber(n):
return ("{:,}".format(n))
# Main code
print(formattedNumber(10))
print(formattedNumber(100))
print(formattedNumber(1000))
print(formattedNumber(10000))
print(formattedNumber(100000))
print(formattedNumber(1234567890))
print(formattedNumber(892887872878))
Output
輸出量
10
100
1,000
10,000
100,000
1,234,567,890
892,887,872,878
翻譯自: https://www.includehelp.com/python/print-number-with-commas-as-thousands-separators.aspx
python逗號分隔符