1

I would like to print the numbers from 1 to 100 to the terminal with print. But I want only one number to be displayed which is steadily incremented.

I know that I can print in the same line by putting a , behind the print statement, but I want to clear the line and print to it again. How can I do this?


Okay, I tried to solve it like here by adding end='\r'

from __future__ import print_function
import time

for x in range(100):
    print(str(x), end='\r')
    time.sleep(0.1)

but that just does nothing.

Community
  • 1
  • 1
vicco
  • 1,049
  • 2
  • 14
  • 33
  • this might help : http://stackoverflow.com/questions/2388090/how-to-delete-and-replace-last-line-in-the-terminal-using-bash – Yondaime008 Dec 16 '15 at 10:57
  • 2
    and upon further search, this question was already asked and answered here : http://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output-python-2-5 – Yondaime008 Dec 16 '15 at 11:00
  • How are you running the script? For example in IDLE this might not be possible. – Martin Evans Dec 16 '15 at 11:05
  • I'm not running it at all. I was just wondering how to print like that. – vicco Dec 16 '15 at 11:07
  • does that answer your question vicco ? If you wrote a piece of code share so we can see where you're getting at and help. – Yondaime008 Dec 16 '15 at 11:08
  • 1
    @vicco on IDLE doesn't work for me, but in terminal it does: `for i in range(10): print '{: 3d}'.format(i), '\r', ; time.sleep(0.1)` -- python 2.7.10 – tglaria Dec 16 '15 at 12:50
  • Very strange, it does work when I execute it in LiClipse, but it does neither work in terminal nor in Idle. – vicco Dec 16 '15 at 13:00

0 Answers0