How do terminal programs update progress bars on the same line?

August 15, 2026 2 answers
Asked by ~courtesy copy!
I'm learning Python and want to make a terminal version of the T-Rex runner game. Programs such as Fedora's package manager show download progress by updating a status bar in place instead of printing a new line for every frame. For example, the progress indicator moves across the same line while the elapsed time changes. How does the terminal do this? I understand that output can be written with echo or print, but normally each update appears on a new line. What technique should I use in Python to refresh the display smoothly?

Answers

well then!
The basic trick is a carriage return, written as `r`. It moves the cursor back to the beginning of the current line without creating a new line, so the next output overwrites the previous frame. In Python, you can do something like `print(f"Progress: {percent}%", end="r", flush=True)`. For more advanced cursor movement, clearing, colors, and multi-line interfaces, terminals provide control sequences. Tools such as `terminfo` and `tput` know which sequences are appropriate for the current terminal, and Python includes the `curses` module for building more complex terminal applications.

PEACH_preserves: A carriage return (CR) is `r`, which returns the cursor to the start of the line. A line feed (LF) is `n`, which moves down to the next line. Using `r` without `n` is what lets you replace the current line instead of adding another one.

Reply to this answer
questionable notation
For a simple progress bar, you usually don't need a full terminal interface. Print the new text with a carriage return and make sure the output is flushed immediately: `import sys, time` `for i in range(101):` ` sys.stdout.write(f"rProgress: {i}%")` ` sys.stdout.flush()` ` time.sleep(0.05)` If a later message is shorter than the previous one, clear or pad the line so leftover characters don't remain visible.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions