I'm taking a Python class right now, and I just learned about the backspace character. Like newline (\n), backspace is a special character with ASCII code 8. My teacher couldn't think of a reason to use that, but I'm curious as to how it's used. Maybe just historical reasons? When I tried print("Hellow\b World")
, I got just got what I expected: Hello World
.
What's the reason for the backspace character, and how might it be used?
Edit: I am aware that it isn't python specific, but when writing the original question I was thinking mainly about Python and forgot this fact. I've tried to edit to make this more clear.
It produces a backspace character. Your terminal backspaced over the second o when printing that character.
The usual way of erasing the last character on the console is to use the sequence "\b \b" . This moves the cursor back one space, then writes a space to erase the character, and backspaces again so that new writes start at the old position. Note that \b by itself only moves the cursor.
The interpretation of the backspace and carriage return characters is left to the software you use for display. A terminal emulator, when displaying \b would move the cursor one step back, and when displaying \r to the beginning of the line.
Backspace is a control character that moves the cursor one character back in the console but doesn't delete it.
What's the reason for the backspace character, and how might it be used?
It was used historically in the ASCII world to print accented characters.
For example à could be produced using the three character sequence a
Backspace
`
(or, using the characters' hex values, 0x61 0x08 0x60
).
See more on backspace here
Backspace key vs Backspace character
A lot of people confuse the two. Backspace key on keyboard has almost the universal function to delete the previous character (= move cursor back and delete that character). The backspace character '\b'
however only moves the cursor one position back in the console window and doesn't delete it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With