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!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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