Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print in Python 3.3.3 without carriage return?

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.

like image 979
RGS Avatar asked Jan 01 '14 01:01

RGS


1 Answers

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.

like image 108
Martijn Pieters Avatar answered Sep 21 '22 21:09

Martijn Pieters