Delete Characters in Python Printed Line
Solution 1:
write('\b') # <-- backup 1-character
Solution 2:
Just to illustrate the great 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")
References
[1] This answer by Sven Marnachin in Python Remove and Replace Printed Items
[2] Blog post about building progress bars
Solution 3:
When you are using print()
, you can set end as \r
, which will replace the text in the line with the new text.
print(text, end="\r")