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