+ 1
How to print contents of a list using loop on a same line?
when I tried to print a list directly, contents of the list were printed as a single line.(with quotes and braces) But when I tried to print using the loop, each content of the list was separated by new line. Is it possible to print contents of a list on a same line using loops? for instance: words=['one','two','three'] for var in words: print(var) Actual output: one two three desired output: onetwothree
4 ответов
+ 3
Another simple way is to join all array items into a single string, and then print it.
Example 1:
words=['one','two','three']
res = ""
for var in words:
res += var
print(res)
Example 2 (using the join method):
words=['one','two','three']
res = "".join(words)
print(res)
+ 2
Yes, there is and to obtain that all you need to do is add another agrument to the print function. Just like so...
words=['one','two','three']
for var in words:
print(var, end=' ')
With that said, when it prints it won't go to a new line. It'll print everything out just like you wanted it.
+ 1
wow that's exactly what I wanted to know! Thanks Dawn!
+ 1
thanks for the suggestions ken kaneki that looks very promising