14

OS: Windows 7

my understanding is that \r moves the text to the left side of the page.

however when I executed this:

carriage_return = "I will use a carriage\rreturn"

print carriage_return

I got: return use a carriage

what I was expecting was: return

user571099
  • 1,491
  • 6
  • 24
  • 42
  • 5
    It just moves back left, it does not erase the line. – Joachim Isaksson Sep 09 '13 at 06:33
  • how did you execute this program? IDLE, CMD, Pycharm. @user571099 – Pie Mar 10 '20 at 18:43
  • The carriage return or \r works differently in different IDEs. Colab will give you the output 'return' that you were expecting while spyder will give you the output 'return use a carriage' (that's is expected from \r) Usually, ` \r` works as if we have shifted your cursor to the beginning of the string or line. If we use this special escape character `\r`, the rest of the content after the `\r` will come at the front of the line and will keep replacing your characters one by one until it takes all the contents left after the `\r` in that string. – Dr Nisha Arora Oct 19 '22 at 02:32

4 Answers4

24

Well, it appears that you did move to the left of the line. It just left the rest of the line untouched. Note that return is 6 chars, and I will is also 6.

Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60
13

\r takes the cursor to the beginning of the line. It is the same effect as in a physical typewriter when you move your carriage to the beginning and overwrite whatever is there.

Seeker
  • 987
  • 1
  • 8
  • 20
  • 3
    Have you ever used a physical typewriter? There is quite a different effect when you do that... :) – Tim Pietzcker Sep 09 '13 at 06:45
  • you mean it scrolls down one line? – Seeker Sep 09 '13 at 09:04
  • 2
    This wasn't an entirely serious comment. I merely meant that if you type over existing text on a typewriter, that text is not replaced with the new one. (Otherwise it would have been hard for me to write an `Ø` on my typewriter). – Tim Pietzcker Sep 09 '13 at 09:54
9

All the characters after the \r escape sequence move to the left and overwrite exactly those number of characters present at the starting of the statement.

In your example, return is there after \r and it is 5 characters and it replaces I will which is also 5 characters (don't forget to include space :P) so it basically replaces I will and prints return use a carriage

Shubham Meshram
  • 175
  • 1
  • 9
7

Just to add to this. If you want to return to the beginning and erase the contents of the line, you can do that like this:

text = "Here's a piece of text I want to overwrite" 
repl = "BALEETED!" # What we want to write
print(text, end="\r") # Write the text and return
print(f'\r{repl: <{len(text)}}')

That last line might need a bit of explaining, so I'll break it down:

f'SOMETHING {var}' is an f-string, equivalent to 'SOMETHING {}'.format('HERE'). It's available in Python 3.6+

So replacing the hard-coded values for variables, we're returning to the beginning and then writing the replacement string, followed by enough spaces to replace the original text. If you want to use the old format method which is probably more clear in this case:

print('\r{0: <{1}}'.format(repl, len(text)))

# Which becomes when extrapolated:
print('BALEETED                                  ')

For bonus points, you don't need to use spaces, you can use anything:

print('\r{0:░<{1}}'.format(repl, len(text)))

# Which becomes when extrapolated:
print('DELETED░░░░░░░░░░░░░░░░░░░░░░░░░░░░░')

Or for extra BONUS make it into a function:

from time import sleep
def overprint(text,repl, t=1, char=" "):
    print(text, end="\r")
    sleep(t) 
    print('\r{0:{1}<{2}}'.format(repl, char, len(text)))

overprint("The bomb will explode in one sec...", "BOOM!")
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
  • Also automatically answers "How to print python3 output on console as a replacing/rolling text ?" – Pe Dro Apr 19 '22 at 14:53
  • To repeat `overprint()` in a loop, using `print('\r{0:{1}<{2}}'.format(repl, char, len(text)), end = "\r")` would be better – Pe Dro Apr 19 '22 at 18:12