Ben Chuanlong Du's Blog

It is never too late to learn.

Update a Line in Standard Output

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

The trick is to use \r (return) instead \n (return and new line). I will use Python to illustrate. The following Python code

:::Python import sys

for idx in range(5): sys.stdout.write(f'\nLine {idx}')

outputs the following lines.

Line 0 Line 1 Line 2 Line 3 Line 4

while the following Python code

:::Python import sys

for idx in range(5): sys.stdout.write(f'\rLine {idx}')

outputs Line i (where i runs from 0 to 4) to the same line in standard output.

Comments