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 ?

24th Feb 2017, 9:09 PM
R00T_L33
R00T_L33 - avatar
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.
24th Feb 2017, 9:35 PM
Jehad Al-Ansari
Jehad Al-Ansari - avatar
+ 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 ^^ )
25th Feb 2017, 1:48 AM
visph
visph - avatar
+ 1
what would you expect the output to be?
24th Feb 2017, 9:32 PM
Cristi Vlad
Cristi Vlad - avatar
+ 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 !
25th Feb 2017, 9:39 AM
R00T_L33
R00T_L33 - avatar
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)
25th Feb 2017, 12:34 AM
R00T_L33
R00T_L33 - avatar