Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Invalid format specifier for thousands place

Writer Mia Lopez

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"

print("{0:20}${1:,20}{2:20}${3:,20}${4:,<5}".format(a,b,c,d,e),file=outfile)

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

print("{0:20}${1:,20}{2:20}${3:20}${4:<5}".format(a,b,c,d,e),file=outfile)
ValueError: Invalid format specifier

What did I do wrong?

1

1 Answer

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'
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.