9

First of all, I am NOT looking for how to print in the same line with the print(some_stuff, end="").

In Python 2.7 you could type:

while True:
for i in ["/","-","|","\\","|"]:
    print "%s\r" % i,

and it would print, in the SAME line, in the SAME spot those 5 characters, making it look like you had a bar spinning (actually you could try it out). The thing is, I can't do the same thing in Python 3.3, and I've tried several things. My specific application would be a countdown timer... The code is something like this:

import time
t = 120
while t > 0:
    t -= 1
    print("Time left till next update: %d seconds" % t)
    time.sleep(1)

where the output should be the string, with ONLY the number of seconds changing in place... Hope someone can help me with this.

Ray
  • 2,472
  • 18
  • 22
RGS
  • 964
  • 2
  • 10
  • 27
  • I think you're mixing up carriage return—the `\r` in your 2.7 code, and the thing that brings the cursor back to the start of the same line—with newline—the `\n` that gets printed automatically, and the thing that advances to the start of the next line. – abarnert Jan 01 '14 at 02:02
  • Sorry, but I didn't quite understand what you said... (It's getting late here, and I'm getting sleepy :) ) but my question has already been answered. Thanks anyway! – RGS Jan 01 '14 at 02:06
  • Your question asks how to print without carriage return. But that's not what you want to do. (If it were, just leave off the `\r`.) – abarnert Jan 01 '14 at 02:10
  • Actually, the point of my question was HOW to do it WITH carriage return... – RGS Jan 01 '14 at 10:37
  • 1
    So why is the title how to do it _without_ carriage return, when the point of the question is the exact opposite of that? – abarnert Jan 02 '14 at 19:03
  • @abarnert I know it's been 7 years... But I have NO idea what I meant I know my English wasn't the best back then, so maybe I confused everyone with my words – RGS Sep 09 '21 at 07:54

2 Answers2

13

You use end='\r' as keyword argument:

print("Time left till next update: %d seconds" % t, end='\r')

The default for end is '\n' but you can specify your own.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I tried it already. That piece of code is just writing one sentence in front of the other... I'm using IDLE, in case that matters... The output I'm getting with that is this: http://prntscr.com/2fiwvo – RGS Jan 01 '14 at 02:01
  • 2
    @RSerrao: I tested this in a terminal and it works just fine. Perhaps IDLE doesn't support `\r` anymore? – Martijn Pieters Jan 01 '14 at 02:02
  • Thank you! THAT is the problem. Just tried in the Power Shell and it is working just fine. – RGS Jan 01 '14 at 02:03
  • Nothing has changed at all. When I test in IDLE, with both 2.7 and 3.3, on three different platforms, `\r` prints a newline instead of a carriage return in console windows and nothing at all in output windows. – abarnert Jan 01 '14 at 02:04
  • 1
    @RSerrao, don't forget to put a blank at the end of the line to erase the last character when the length of the count changes. – Mark Ransom Jan 01 '14 at 02:09
0

You can also try lower level like: os.write(fd, data) on fd=1 (sys.stdin).
Note that data must be bytes (b'whatever data').

import os, time
os.write(1, b'Time left till next update: 120 seconds'+b'\b'*8)
t=120
while t>0:
    time.sleep(1)
    t -= 1
    os.write(1, b'\b'*3+str(t).zfill(3).encode())
print()

It works properly in the terminal but Python IDLE ignores '\r' and '\b'.

DuniC
  • 243
  • 2
  • 7