Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python format string thousand separator with spaces

For printing number with thousand separator, one can use the python format string :

'{:,}'.format(1234567890) 

But how can I specify that I want a space for thousands separator?

like image 439
wonzbak Avatar asked May 21 '13 12:05

wonzbak


People also ask

How do you add a thousand separator in Python?

Using the modern f-strings is, in my opinion, the most Pythonic solution to add commas as thousand-separators for all Python versions above 3.6: f'{1000000:,}' . The inner part within the curly brackets :, says to format the number and use commas as thousand separators.

What does %S and %D do in Python?

They are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator.

What is %s %d %F in Python?

Answer. In Python, string formatters are essentially placeholders that let us pass in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers. If you provide a float value, it will convert it to a whole number, by truncating the values after the decimal point.


1 Answers

Here is bad but simple solution if you don't want to mess with locale:

'{:,}'.format(1234567890.001).replace(',', ' ') 
like image 170
Raz Avatar answered Sep 28 '22 06:09

Raz