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"

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

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.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest