Consider a simpler example:
>>> print('test')
test
>>>
Now add end=''
to suppress the newline:
>>> print('test', end='')
test>>>
Without the newline, the prompt is displayed immediately after the string test
; the cursor was never moved to the next line.
Now add the carriage return:
>>> print('\r', 'test', end='')
test>>>
The \r
doesn't really do anything: it causes the cursor to move to the beginning of the current line, but the cursor is already there. However, print
does output a space before printing the next string, and as before, no newline is printed to move the cursor to the next line.
Here's an example where \r
does have an effect. Compare
>>> print('foo', 'bar', end='')
foo bar>>>
with
>>> print('123456789', '\r', 'bar', end='')
bar>>> 9
123456789
is printed, but the \r
moves the cursor so that the space and bar
overwrites the 1234
. The prompt and following space output by the interpreter itself overwrites 5678
.