0

I want to print some values using str.format. However, I am getting the first value repeated, which I do not understand. Any help will be appreciated.

for tel in ['T1', 'T2', 'T3', 'T4']:
    print(tel+':{0:.2f}, {0:.2f}, {0:.2f}'.format(0.56, 0.12, 0.25))

3 Answers 3

4

That's because of the zero in {0:.2f} which means 'argument at index 0', so you call always the same, just remove it, and it'll use arguments in their order

for tel in ['T1', 'T2', 'T3', 'T4']:
    print(tel + ':{:.2f}, {:.2f}, {:.2f}'.format(0.56, 0.12, 0.25))
Sign up to request clarification or add additional context in comments.

Comments

3

The first entry in curly braces is the index of the list you reference in the format statement. So you always take the first entry by saying {0:x}. What you need to do is:

print(tel+':{0:.2f}, {1:.2f}, {2:.2f}'.format(0.56, 0.12, 0.25))

or leave it empty like {:x}.

Comments

1

The number before : is the index of the argument in format() so you are selecting the first argument in all three places. You should use {:.2f} instead of {0:.2f}.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.