banjocode Print On The Same Line With Python

Print On The Same Line With Python

Printing on the same line with Python is useful if you, for example, want to use a progress bar or download status in a CLI program. This is a simple way to do that.

1 min read

Printing on the same with Python is not that difficult, it did, however, take some time before I managed to find the best way to do it myself. This is a neat little trick and I tend to use it quite a lot in my programs.

print("hello", end="\r", flush=True)

Example

I have written a quick example, with the following code:

import time

for i in range(10):
    print(i, end="\r", flush=True)
    time.sleep(0.5)

It looks like this.

Flush python