How to overwrite the previous print to stdout?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs
--
Chapters
00:00 How To Overwrite The Previous Print To Stdout?
00:21 Answer 1 Score 18
00:34 Accepted Answer Score 244
02:20 Answer 3 Score 112
02:37 Answer 4 Score 36
03:19 Thank you
--
Full question
https://stackoverflow.com/questions/5419...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
ACCEPTED ANSWER
Score 244
Simple Version
One way is to use the carriage return ('\r') character to return to the start of the line without advancing to the next line.
Python 3
for x in range(10):
    print(x, end='\r')
print()
Python 2.7 forward compatible
from __future__ import print_function
for x in range(10):
    print(x, end='\r')
print()
Python 2.7
for x in range(10):
    print '{}\r'.format(x),
print
Python 2.0-2.6
for x in range(10):
    print '{0}\r'.format(x),
print
In the latter two (Python 2-only) cases, the comma at the end of the print statement tells it not to go to the next line. The last print statement advances to the next line so your prompt won't overwrite your final output.
Line Cleaning
If you can’t guarantee that the new line of text is not shorter than the existing line, then you just need to add a “clear to end of line” escape sequence, '\x1b[1K' ('\x1b' = ESC):
for x in range(75):
    print('*' * (75 - x), x, end='\x1b[1K\r')
print()
Long Line Wrap
All these methods assume you’re not writing more than the length of the line. The carriage return only returns to the start of the current line, so if your output is longer than a line, you’ll only erase the last line.
If this is enough of a problem that you need to control it, you can disable line wrapping to keep the cursor from wrapping to the next line. (Instead, the cursor sticks to the end of the line, and successive characters overwrite.)
Line wrap is disabled with print('\x1b[7l', end='') and re-enabled with print('\x1b[7h', end=''). Note that there is no automatic re-enable of line wrap at any point: don’t leave the terminal broken if an exception ends your program!
ANSWER 2
Score 112
Since I ended up here via Google but am using Python 3, here's how this would work in Python 3:
for x in range(10):
    print("Progress {:2.1%}".format(x / 10), end="\r")
Related answer here: How can I suppress the newline after a print statement?
ANSWER 3
Score 36
@Mike DeSimone answer will probably work most of the time. But...
for x in ['abc', 1]:
    print '{}\r'.format(x),
-> 1bc
This is because the '\r' only goes back to the beginning of the line but doesn't clear the output.
If POSIX support is enough for you, the following would clear the current line and leave the cursor at its beginning:
print '\x1b[2K\r',
It uses ANSI escape code to clear the terminal line. More info can be found in wikipedia and in this great talk.
Other approach
This other, (arguably worse) solution I have found looks like this:
last_x = ''
for x in ['abc', 1]:
    print ' ' * len(str(last_x)) + '\r',
    print '{}\r'.format(x),
    last_x = x
-> 1
One advantage is that it will work on windows too.
ANSWER 4
Score 18
Suppress the newline and print \r.
print 1,
print '\r2'
or write to stdout:
sys.stdout.write('1')
sys.stdout.write('\r2')