+ 5

What is the problem in this code. Plz explain.

Please first don't share code..give the step by step guide so that I can correct my mistake.👇 https://code.sololearn.com/cSnNOGqvSmO9/?ref=app

1st Sep 2020, 8:08 AM
riya charles
riya charles - avatar
12 Réponses
+ 7
using f- string should be the preferred way to make formatted output. # >>> there is no need to use multiple f-strings, as you can can use {multiple expressions} in one f-string nums=[4,5,6] msg= f"Numbers: {nums[0]}{nums[1]}{nums[2]}" print(msg) # or do it this way: print(f"Numbers: {nums[0]}{nums[1]}{nums[2]}") # or with basic print and unpack operator print('Numbers: ',*nums,sep='')
1st Sep 2020, 11:01 AM
Lothar
Lothar - avatar
+ 5
Thank u lothar 😊. I understood
1st Sep 2020, 12:37 PM
riya charles
riya charles - avatar
+ 4
Thank u all for your help I'll correct it now.
1st Sep 2020, 8:19 AM
riya charles
riya charles - avatar
+ 4
riya charles Adding onto what Md. Nasif-ur-Rahman Rimon, I’ve updated my answer with a more compact way of doing it with the splat operator. Refer to my previous answer. 😁😁😁
1st Sep 2020, 8:22 AM
Rowsej
Rowsej - avatar
+ 4
Thank u 😊
1st Sep 2020, 8:25 AM
riya charles
riya charles - avatar
+ 4
Thank u sanjay kamath 🙂
2nd Sep 2020, 7:37 AM
riya charles
riya charles - avatar
+ 4
Ok I'll try it..thank u B.Balaji. 🙂
2nd Sep 2020, 1:27 PM
riya charles
riya charles - avatar
+ 3
riya charles Adding onto what Md. Nasif-ur-Rahman Rimon, you should change line 4 to: msg = msg.format(nums[0], nums[1], nums[2]) EDIT: Or, to simplify: msg = msg.format(*nums) Using *nums is the splat operator, similar to the JS spread operator.. Reference: https://stackoverflow.com/questions/48451228/how-to-spread-a-JUMP_LINK__&&__python__&&__JUMP_LINK-array
1st Sep 2020, 8:15 AM
Rowsej
Rowsej - avatar
+ 3
nums=[4,5,6] msg_1= f"Numbers {nums[0]} {nums[1]} {nums[2]}" msg_2 ="Numbers {} {} {}".format(*nums) msg_3 = "Numbers %i %i %i" %(nums[0],nums[1],nums[2]) print(msg_1) print(msg_2) print(msg_3)
1st Sep 2020, 9:19 AM
Jenson Y
2nd Sep 2020, 7:36 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 3
# you are repeating your code. # try this code. nums = [4,5,6] print(f"Numbers: {nums[0] + nums[1] + nums[2]}") print(f"Numbers: {nums[0]}, {nums[1]}, {nums[2]}")
2nd Sep 2020, 1:23 PM
Balaji
Balaji - avatar
+ 2
Line 2 Not :(semicolon) that is = Line 4: format function not come in next line. That come in end of line 4. There not is =, {} that is () and beginning with .(dot) at end of line 3. msg = 'Number:{0},{1},{2}'.format(nums[0],nums[1],nums[2])
1st Sep 2020, 8:15 AM
Vadivelan