0

I am trying to output text to stdout, overwriting the previous text, for example

for i in range(12):
    print i

but with i replacing the previous value each time rather than appearing on a new line From looking at quite a few previous posts with similar questions it seems that there are a few ways of doing this, possibly the simplest being (for Python 3.x on)

for i in range(12):
    print(i,end="\r")

sometimes with a comma at the end of the print statement, sometimes not. However, without the comma I get no output at all and with the comma I get

(None,)
(None,)
(None,)
...

Is this something related to my terminal perhaps? I get similar results no matter which of the previous posted solutions to the problem I try. Thanks for any help!

1 Answers1

2

If you do this in a terminal you won't see any output because the for loop finishes too fast for you to see the output changing. After the loop is done the terminal's prompt overwrites the output.

Try this instead:

>>> for i in range(9999999):
...     print(i, end="\r")

I'm surprprised you don't get a syntax error when omitting the comma.

MB-F
  • 22,770
  • 4
  • 61
  • 116
  • 2
    I think when the OP talks about putting a comma at the end, he means `print(i, end="\r"),`. That would construct a 1-element tuple containing the result of the `print` call, and since this is in the interactive terminal, the tuple gets printed. – user2357112 Dec 20 '13 at 09:11
  • That's great, that's exactly what was happening. Thanks! Also, yes I meant print(i, end="\r"), by 'comma at the end' – user3119998 Dec 20 '13 at 09:21