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) + " ")
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
+ 2
Jayakrishnaš®š³ please do well to explain the last line
+ 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ā
+ 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 = ", .")
+ 2
Jayakrishnaš®š³ Thanks a lot.
Are the sep and end attributes taught in the python core course here in sololearn?
+ 2
Am not sure, is it there or not. But you can get from documention or in interpreter by
help(print)
+ 2
Alvinā Note: sep and end aren't attributes, they're named parameters
+ 1
qwerty = [10, 20, 30, 40]
for w in qwerty:
print(str (w) + " ",end="")
#Note : default is end="\n" so override end=""
+ 1
qwerty = [10, 20, 30, 40]
need = ""
for i in range(len(qwerty)):
need = need + str(qwerty[i]) + " "
print (need)
+ 1
Another way:
qwerty = [10, 20, 30, 40]
str_qwerty = srt(qwerty)
str_qwerty.replace("[", "")
str_qwerty.replace("]", "")
print (str_qwerty)
+ 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)
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] + ".")