How do I print in Python 3.3.3 without carriage return?

11,489

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.

Share:
11,489

Related videos on Youtube

RGS
Author by

RGS

I am a regular student who likes to code, loves to learn and is passionate about mathematics! Check out mathspp.com for my projects on maths and programming.

Updated on September 16, 2022

Comments

  • RGS
    RGS over 1 year

    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.

    • abarnert
      abarnert over 10 years
      So why is the title how to do it without carriage return, when the point of the question is the exact opposite of that?
  • RGS
    RGS over 10 years
    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: prntscr.com/2fiwvo
  • Martijn Pieters
    Martijn Pieters over 10 years
    @RSerrao: I tested this in a terminal and it works just fine. Perhaps IDLE doesn't support \r anymore?
  • RGS
    RGS over 10 years
    Thank you! THAT is the problem. Just tried in the Power Shell and it is working just fine.
  • abarnert
    abarnert over 10 years
    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.
  • Mark Ransom
    Mark Ransom over 10 years
    @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.
  • RGS
    RGS over 10 years
    @MarkRansom Good tip ;)