Flush output in for loop in Jupyter notebook
The first answer is correct but you don't need sys
package. You can use the end
parameter of the print
function. It specifies what to print at the end, and its default value is \n
(newline) (docs1, docs2). Use \r
(carriage return) instead.
import time
for i in range (10):
print(i, end="\r")
time.sleep(0.5) # This line is to see if it's working or not
Try this
for i in range (10):
print("\r", end=str(i))
#Try this:
import sys
import time
for i in range (10):
sys.stdout.write('\r'+str(i))
time.sleep(0.5)