+ 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
12 Respuestas
+ 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='')
+ 5
Thank u lothar 😊. I understood
+ 4
Thank u all for your help I'll correct it now.
+ 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.
😁😁😁
+ 4
Thank u 😊
+ 4
Thank u sanjay kamath 🙂
+ 4
Ok I'll try it..thank u B.Balaji. 🙂
+ 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
+ 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)
+ 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]}")
+ 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])