I want to print the items in a single line like this: 10 20 30 40 . Not on separate lines, but it prints on separate lines. Why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I want to print the items in a single line like this: 10 20 30 40 . Not on separate lines, but it prints on separate lines. Why?

qwerty = [10, 20, 30, 40] for w in qwerty: print(str (w) + " ")

28th Jan 2022, 9:19 AM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
13 Answers
+ 3
qwerty = [10, 20, 30, 40] for w in qwerty: print (str (w) , sep=" ,",end = ".") # Alvin✓ sep attribute for separating values #end attribute used to set end point as shown
28th Jan 2022, 9:47 AM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 please do well to explain the last line
28th Jan 2022, 9:22 AM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
+ 2
default value is end="\n" so override end="" . It just cause to put "" instead of \n check this in playground for print syntax and explanation of more attributes. hope it clears help(print) #Alvin✓
28th Jan 2022, 9:24 AM
Jayakrishna 🇮🇳
+ 2
But how do I put a full stop at the end? After putting commas in between the items qwerty = [10, 20, 30, 40] for w in qwerty: print (str (w) + " ", end = ", .")
28th Jan 2022, 9:42 AM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
+ 2
Jayakrishna🇮🇳 Thanks a lot. Are the sep and end attributes taught in the python core course here in sololearn?
28th Jan 2022, 9:50 AM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
+ 2
Am not sure, is it there or not. But you can get from documention or in interpreter by help(print)
28th Jan 2022, 9:52 AM
Jayakrishna 🇮🇳
+ 2
Alvin✓ Note: sep and end aren't attributes, they're named parameters
28th Jan 2022, 2:23 PM
Œ ㅤ
Œ ㅤ - avatar
+ 1
qwerty = [10, 20, 30, 40] for w in qwerty: print(str (w) + " ",end="") #Note : default is end="\n" so override end=""
28th Jan 2022, 9:21 AM
Jayakrishna 🇮🇳
+ 1
Œ ㅤ are they built-in keywords in python?
29th Jan 2022, 9:19 PM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
+ 1
qwerty = [10, 20, 30, 40] need = "" for i in range(len(qwerty)): need = need + str(qwerty[i]) + " " print (need)
29th Jan 2022, 11:33 PM
Алексей Молодан
Алексей Молодан - avatar
+ 1
Another way: qwerty = [10, 20, 30, 40] str_qwerty = srt(qwerty) str_qwerty.replace("[", "") str_qwerty.replace("]", "") print (str_qwerty)
29th Jan 2022, 11:39 PM
Алексей Молодан
Алексей Молодан - avatar
+ 1
Alvin✓ They aren't keywords. Keywords make up a separate statement, not a part of a function call. It's just that two of the function's parameters are named and can be passed using their names, end and sep (which eliminates the burden of remembering the order of giving the args)
30th Jan 2022, 2:19 AM
Œ ㅤ
Œ ㅤ - avatar
0
Hey Jayakrishna🇮🇳 I've found a solution to put a full stop at the end 🥳😃. Try this 👇🏻 qwerty = [10, 20, 30, 40] for x in qwerty: print (str (x), end = ", ") if x == qwerty[-1]: print (str (qwerty[-1] + ".")
4th Feb 2022, 7:38 PM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar