heej heej - 1 year ago 248
Python Question

Invalid format specifier for thousands place

I am trying to make a simple chart like output. Here are strings that I want to display:

a = "name", b = "10000.00", c = "code", d = "45.60", e = "30.00"


I put "," to indicate thousands place in each format specifier in which I want them to be output as currency. It reports the error:

ValueError: Invalid format specifier

What did I do wrong?

Answer Source

As per the docs, width has to go after comma. Moreover, your b variable has to be a number (and not a string, as in your MWE):

>>> x = 10000.0
>>> '{0:20,}'.format(x)
'            10,000.0'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download