+ 7

vals=[2,4,7] new="{1}{2}{1}".format(vals[0],vals[2],vals[1]) print(new)

Needs reason of output

10th Jun 2020, 6:43 PM
Abhishek Kumar
Abhishek Kumar - avatar
6 Answers
+ 8
new="{1}{2}{1}".format(2,7,4) Inside the Curly brackets we write the index no. Whose value we want,so we will get new="747"
10th Jun 2020, 6:52 PM
ANJALI SAHU
+ 7
đŸ˜‚đŸ˜‚đŸ€ŁđŸ€ŁđŸ€Łno man your answer is best
10th Jun 2020, 7:07 PM
Abhishek Kumar
Abhishek Kumar - avatar
+ 7
Why not using an f-string? It is much more clear and concise, and can do more than other formatting methods. print(f'{vals[2]}{vals[1]}{vals[2]}') You don't need placeholders and a format(), it can be done just how it's shown here. If it is used without print, it can generate a string, that looks like the output.
10th Jun 2020, 8:27 PM
Lothar
Lothar - avatar
10th Jun 2020, 7:53 PM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 1
😅😅
10th Jun 2020, 7:08 PM
ANJALI SAHU
+ 1
In curly brackets index is given so at index 1in vals is 7 and index 2 is 4 and finaly index 1 is 7 so output of code or new="747"
4th Apr 2021, 3:09 PM
Amrit
Amrit - avatar