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!
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!
I surprised no one has mentioned the pre-Python3 method for suppressing the newline: a trailing comma.
This does insert spaces before certain characters, as is explained here.
This can be easily done with the print() function with Python 3.
will give you
In Python v2 you can use the
print()
function by including:as the first statement in your source file.
As the print() docs state:
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:
or
For Python3: