+ 1

https://code.sololearn.com/c2UYoGCKIOu9/?ref=app can anyone justify and tell why the output is coming like this? #python #string

Results are very random (at least to me when I tried playing with the codes).

26th Oct 2019, 4:29 AM
Akash Singh
Akash Singh - avatar
4 Réponses
+ 2
Akash Singh this is because you have put different indexes in placeholders{} and when placeholders{} are empty, Python will replace the values passed through str.format in order. The values that exist within the str.format() method are essentially tuple data types(in your case it is [ nums[0],nums[2],nums[1] ]) and each individual value contained in the tuple can be called by its index number, which starts with the index number 0. These index numbers can be passes into the curly braces that serve as the placeholders in the original string. So that's why when you type {0} then it is considered as first index of that tupple
26th Oct 2019, 4:53 AM
Arsenic
Arsenic - avatar
+ 1
https://code.sololearn.com/c2UYoGCKIOu9/?ref=app Use above link to see the code.
26th Oct 2019, 4:31 AM
Akash Singh
Akash Singh - avatar
+ 1
Arsenic I have the basic idea about the string {} and format [ ] . Can you elaborate a little more and answer why first output value is 6, cause this is neither 1st index of { } nor [ ]?
26th Oct 2019, 5:16 AM
Akash Singh
Akash Singh - avatar
+ 1
Your code: nums = [4, 5, 6] print("Numbers: {1} {2} {0}". format(nums[0], nums[2], nums[1])) The result: 6 5 4 To understand this, simply replace num[0], num[1] and num[2] with their values: print("Numbers: {1} {2} {0}". format(4, 6, 5))
26th Oct 2019, 5:55 AM
Bilbo Baggins
Bilbo Baggins - avatar