I would like to make several statements that give standard output without seeing newlines in between statements.
Specifically, suppose I have:
for item in range(1,100): print item
The result is:
1 2 3 4 . . .
How get this to instead look like:
1 2 3 4 5 ...
Even better, is it possible to print the single number over the last number, so only one number is on the screen at a time?
print item to:
print item,in Python 2.7
print(item, end=" ")in Python 3
If you want to print the data dynamically use following syntax:
print(item, sep=' ', end='', flush=True)in Python 3
By the way...... How to refresh it every time so it print mi in one place just change the number.
In general, the way to do that is with terminal control codes. This is a particularly simple case, for which you only need one special character: U+000D CARRIAGE RETURN, which is written
'\r' in Python (and many other languages). Here's a complete example based on your code:
from sys import stdout from time import sleep for i in range(1,20): stdout.write("\r%d" % i) stdout.flush() sleep(1) stdout.write("\n") # move the cursor to the next line
Some things about this that may be surprising:
\rgoes at the beginning of the string so that, while the program is running, the cursor will always be after the number. This isn't just cosmetic: some terminal emulators get very confused if you do it the other way around.
stdout.flushis necessary on some systems, or you won't get any output. Other systems may not require it, but it doesn't do any harm.
If you find that this doesn't work, the first thing you should suspect is that your terminal emulator is buggy. The vttest program can help you test it.
You could replace the
stdout.write with a