Multiple arguments for string formatting


I am working on an exercise for ‘Illustrated guide to python’. and cant figure out how to add more than one argument when formatting a string. I have tried different placements and separation and can get them to work one at a time no problem.

here is the example.

car = "{:<10}".format("Car")
cost = "{:,}{:>10}".format(13499.99)

i am getting this error:

cost = “{:,}{:>10}”.format(13499.99)
IndexError: tuple index out of range

the book doesn’t really give too much explanation and the help function doesn’t mention it. Any help would be appreciated.

1 Like

There are two placeholders and only one argument.

1 Like

ok, i’ve used to wrong language when describing the problem.
so if i wanted to both format the number with commas and align could I put the format inside the same placeholder?
like :
{{:,} {:>10}}

1 Like

Did you try that? It’s one (or the only) way to see if your ideas will work or not. Try them. We won’t break the computer, or the interpreter, just generate error messages that help us to learn.

Did you examine the sample I posted? Its output was,

' 13,499.99'
1 Like

Yeah I got it to work by copying what you posted. I cant get it to work as I add more formatting but I will figure it out.

1 Like

Rather than storing formatted values, keep your data natural and format when you print.

car = "Car"
cost = 13499.99
print ("{...}, {...}".format(car, cost))

... means fill in the blanks.

1 Like