I was wondering if there is a way to print elements without newlines such as
x=['.','.','.','.','.','.']
for i in x:
print i
and that would print ........
instead of what would normally print which would be
.
.
.
.
.
.
.
.
Thanks!
This can be easily done with the print() function with Python 3.
for i in x:
print(i, end="") # substitute the null-string in place of newline
will give you
......
In Python v2 you can use the print()
function by including:
from __future__ import print_function
as the first statement in your source file.
As the print() docs state:
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
Note, this is similar to a recent question I answered ( https://stackoverflow.com/a/12102758/1209279 ) that contains some additional information about the print()
function if you are curious.
As mentioned in the other answers, you can either print with sys.stdout.write, or using a trailing comma after the print to do the space, but another way to print a list with whatever seperator you want is a join:
print "".join(['.','.','.'])
# ...
print "foo".join(['.','.','.'])
#.foo.foo.