How to reset cursor to the beginning of the same line in Python

import sys, time

for i in xrange(0, 101, 10):
  print '\r>> You have finished %d%%' % i,

The \r is the carriage return. You need the comma at the end of the print statement to avoid automatic newline. Finally sys.stdout.flush() is needed to flush the buffer out to stdout.

For Python 3, you can use:

print("\r>> You have finished {}%".format(i), end='')

Leave a Comment