How to print contents of a list using loop on a same line? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

4th Jan 2017, 4:45 AM
heduson satchi
heduson satchi - avatar
4 Answers
+ 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)
4th Jan 2017, 7:35 AM
Dao
Dao - avatar
+ 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.
4th Jan 2017, 5:38 AM
Don
Don - avatar
+ 1
wow that's exactly what I wanted to know! Thanks Dawn!
4th Jan 2017, 5:47 AM
heduson satchi
heduson satchi - avatar
+ 1
thanks for the suggestions ken kaneki that looks very promising
6th Jan 2017, 12:28 AM
heduson satchi
heduson satchi - avatar