Removing characters in a printable Python string

I am writing a program where I want the cursor to print letters on one line, but then delete them as if the person were typing, making a mistake, deleting back to the error and continuing to type there.

All I have so far is the ability to write them on one line:

import sys, time
write = sys.stdout.write

for c in text:  
    write(c)
    time.sleep(.5)
+5
source share
2 answers
write('\b')  # <-- backup 1-character
+13
source

Just to illustrate the excellent answers given by @ user590028 and @Kimvais

sys.stdout.write('\b') # move back the cursor
sys.stdout.write(' ')  # write an empty space to override the
                       # previous written character.
sys.stdout.write('\b') # move back the cursor again.

# Combining all 3 in one shot: 
sys.stdout.write('\b \b')

# In case you want to move cursor one line up. See [1] for more reference.
sys.stdout.write("\033[F")

Links
[1] This answer is by Sven Marnachin on Python Remove and Replace Printed Items
[2] Blog Post on Progress Steps

+2
source

All Articles