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!
In short: You can't. raw_input() will always echo the text entered by the user, including the trailing newline. That means whatever the user is typing will be printed to standard output. If you want to prevent this, you will have to use a terminal control library such as the curses module.
To print on the same line in Python, add a second argument, end=' ', to the print() function call.
In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.
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.
import sys
for i in x:
sys.stdout.write(i)
or
print ''.join(x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With