Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Characters in Python Printed Line

Tags:

python

I'm writing a program where I want the cursor to print letters on the same line, but then delete them as well, as if a person was typing, made a mistake, deleted back to the mistake, and kept typing from there.

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

import sys, time
write = sys.stdout.write

for c in text:  
    write(c)
    time.sleep(.5)
like image 203
JShoe Avatar asked Mar 30 '12 20:03

JShoe


People also ask

How do you delete a printed character in Python?

Python Remove Character from String using translate() Python string translate() function replace each character in the string using the given translation table. We have to specify the Unicode code point for the character and 'None' as a replacement to remove it from the result string.

How do I remove characters from text in Python?

You can remove a character from a Python string using replace() or translate(). Both these methods replace a character or string with a given value. If an empty string is specified, the character or string you select is removed from the string without a replacement.

What does '\ r mean in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

How do I remove all characters from a string in Python?

Python Remove Character from a String – How to Delete Characters from Strings. In Python you can use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result.


1 Answers

write('\b')  # <-- backup 1-character
like image 95
user590028 Avatar answered Oct 27 '22 01:10

user590028