I was wondering if I could have some code which would delete a printed sentence in the python shell after a delay.
example:
print("hello")
time.sleep(1)
then some kind of code that after the one second delay deletes print("hello")
fyi, I use IDLE python 3.7 on windows 10
Asked
Active
Viewed 53 times
0

Elessar
- 3
- 2
-
2Possible duplicate of [Remove and Replace Printed items](https://stackoverflow.com/questions/5290994/remove-and-replace-printed-items) – Gino Mempin Nov 30 '18 at 03:25
1 Answers
0
>>> import time
>>> import sys
>>> def maybe_hi():
... print('hi', end='') # Don't print the newline yet
... sys.stdout.flush() # Make sure this actually prints now.
... time.sleep(1)
... print('\r ') # Return the carriage and overwrite with spaces.
...
>>> maybe_hi()
This usually works in terminal applications, but some of them don't handle carriage returns properly. I don't think this works in IDLE, for instance. You could also try \b
a few times to back up the cursor one character at a time. For more advanced console shenanigans, check out curses.

gilch
- 10,813
- 1
- 23
- 28