Is there a way to print a backspace in Python without using \b?

555 Views Asked by At

I want to make a typing effect but I can't print a backspace. I tried using print('\b') but it just printed out an x-ed out square. Are there any other ways to print a backspace? If it helps I'm using Mu as my code editor.

1

There are 1 best solutions below

0
ShadowRanger On

The problem is running the script in your IDE, whose terminal (apparently) doesn't support backspacing to erase characters (it's not unusual in this; the IDLE IDE that ships with Python doesn't support backspaces in output either). Run it in a regular terminal (e.g. cmd.exe on Windows, bash or the like on a standard *NIX terminal), and printing '\b' works. It's all up to the terminal how it supports (or doesn't support) backspacing.

For the record, you'd want to use either:

 print('\b', end='')  # Optionally add flush=True to force it out immediately

or:

 sys.stdout.write('\b')
 # Optionally:
 sys.stdout.flush()  # to force it out immediately

to avoid the trailing newline (backspacing over a character shouldn't force you to the next line as a side-effect).