+ 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