Skip to content
Advertisement

how to print something in the terminal for limited rime

I want to print random number in the terminal for just a few second

number = 10
print(number)

but I can’t delete it after its display in terminal. is there any way to hide number after it show in terminal?

Advertisement

Answer

You can go back to the beginning of the line with r and flush=True, then overwrite the content of the line.

import time
print("secret message", end="r", flush=True)
time.sleep(3)
print(" " * 20)

For more complex behaviour, including clearing more that just the current line, you should check out curses.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement