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?
To make the numbers overwrite each other, you can do something like this:
That should work as long as the number is printed in the first column.
EDIT: Here's a version that will work even if it isn't printed in the first column.
I should note that this code was tested and works just fine in Python 2.5 on Windows, in the WIndows console. According to some others, flushing of stdout may be required to see the results. YMMV.
Change
print item
to:print item,
in Python 2.7print(item, end=" ")
in Python 3If you want to print the data dynamically use following syntax:
print(item, sep=' ', end='', flush=True)
in Python 3Like the other examples,
I use a similar approach but instead of spending time calculating out the last output length, etc,
I simply use ANSI code escapes to move back to the beginning of the line and then clear that entire line before printing my current status output.
To use in your iteration loop you would just call something like:
See more here
for Python 2.7
for Python 3
I think a simple join should work: