0
I dont unserstand the output if this code : # string formatting nums = [4, 5, 6] msg = "Numbers: {0} {0} {2}". format(nums[0], nums[1], nums[2]) print(msg) 4 4 6 but when I do : # string formatting nums = [4, 5, 6] msg = "Numbers: {0} {0} {2}". format(nums[1], nums[1], nums[2]) print(msg) it gives me 5 5 6 is there a priority in the Reading ?
5 odpowiedzi
+ 3
It's because your format string is: "Numbers: {0} {0} {2}".
Note {0} is used twice.
Therefore, only the first and third item given to "format" will be used. The second item is not used.
+ 2
You can also use brakets format without indexes, and the replacements will occurs in order of parameters:
"Numbers: {} {} {}".format(v1,v2,v3)
... will output 'Numbers: v1 v2 v3' ( obviously, where vn are values of ^^ )
+ 1
what would you expect the output to be?
+ 1
oh thats actually convenient if you dont want to messup with indexes but I Guess it only works if you dont care about setting a particular ordre yo tour output. thanks a lot guys !
0
i was expecting an error. yeah Ok i got it Now. thanks Jahed. I was confusing the indexing of the list and the indexing of Whats inside the format(parenthesis, damn,it)