Python: for loop - print on the same line
Use end
parameter in the print
function
print(new_item, end=" ")
There is another way to do this, using comprehension and join
.
print (" ".join([function(word) for word in split]))
The simplest solution is using a comma in your print
statement:
>>> for i in range(5):
... print i,
...
0 1 2 3 4
Note that there's no trailing newline; print
without arguments after the loop would add it.