whats wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

whats wrong with my code?

a= [ 1, 2, 3, 4, 5] b = "{2}".format(a[2]) print(b)

7th Jun 2020, 6:29 PM
sofronis
sofronis - avatar
4 Answers
+ 7
whats wrong with my code? a= [ 1, 2, 3, 4, 5] b = "{0}".format(a[2]) print(b)
7th Jun 2020, 6:40 PM
Oma Falk
Oma Falk - avatar
+ 5
In this context, "{2}" denotes the third argument of the format() method, but it only contains one - a[2]. Change {2} to {0} or just {}.
7th Jun 2020, 6:40 PM
Russ
Russ - avatar
+ 4
sofronis You don't have to use format() at all. If you're only doing this, you can just say print(a[2]) But to show you how to use it, observe this example: print("{0}{1}{0}".format("abra", "cad") # abracadabra {0} refers to the first argument passed to format(), {1} refers to the second, and so on if you have more.
7th Jun 2020, 7:09 PM
Russ
Russ - avatar
+ 1
so i always put {0} at first, and {number} for the number i want
7th Jun 2020, 7:02 PM
sofronis
sofronis - avatar