What is the use of '\b' (backspace)
Asked Answered
A

5

17

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.

Armandoarmature answered 1/2, 2018 at 17:51 Comment(3)
Possible duplicate of : https://mcmap.net/q/743883/-using-b-in-python-3Labyrinthine
"why this was added to Python" - Python doesn't decide how ASCII or Unicode are defined.Mithridatism
@Labyrinthine that question is debugging the \b char, but I'm more interested in the real-life applications of this (or if there aren't any, then the historical reasons for it).Armandoarmature
S
23

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.

Seafarer answered 13/6, 2019 at 14:19 Comment(0)
J
7

Maybe it helps to first understand what is happening there:

print() is writing to the standard output and it is writing everything there including the w and the backspace.

Now something has to display it. Most likely a terminal emulator.

What theoretically would happen is that the w was displayed and then deleted, but it was not flushed and was to fast for it to actually happen.

So real-life applications will almost always use \b at the beginning of the printed text.

I wrote a short example that will have a little spinner on a progress indicator. The example print "-" followed by "\b\\" (deleting the - and replacing it with \) followed by "\b|" (deleting the \ and replacing it with |) and so on.

That way - \ | / - \ | / looks like an animated rotating line.

#!/usr/bin/env python3
import time

spinner="\\|/-"
print ("----------\r", end='', flush=True)
for pos in range(10):
    print ("-", end='', flush=True)
    for spin in range(25):
        #here should be a break as soon as some task proceeded further
        time.sleep(0.1)
        print ("\b" + spinner[spin % 4], end='', flush=True)
    print ("\b*", end='', flush=True)
print ()

P.S.: A lot of existing programs use control characters like \b \r \033 to display a status line. Most popular is probably wget. I have also seen such output by at least one python script (although I cant remember which one any more)

Johns answered 2/8, 2018 at 10:26 Comment(0)
B
3

This is not a feature of Python, but a symbol defined by ASCII. Python just supports it (like all other languages).

More specifically, it is a control character that is used either to erase the last character printed or to overprint it. The first version of ASCII was published in 1963 when the common way to output symbols was to send them to a printer and physically print the letters on paper. Here's an excerpt from Wikipedia:

Printing control characters were first used to control the physical mechanism of printers, the earliest output device. [...] The backspace character (BS) moves the printing position one character space backwards. On printers, this is most often used so the printer can overprint characters to make other, not normally available, characters. On terminals and other electronic output devices, there are often software (or hardware) configuration choices which will allow a destruct backspace (i.e., a BS, SP, BS sequence) which erases, or a non-destructive one which does not.

Ber answered 1/2, 2018 at 17:59 Comment(0)
J
1

A small example of how it works:

>>> name = "Robert" + "\b\b\b\b" + 'p' + "\b\b" + 'u'
>>> print(text)
Rupert
>>> print(list(text))
['R', 'o', 'b', 'e', 'r', 't', '\x08', '\x08', '\x08', '\x08', 'p', '\x08', '\x08', 'u']
>>> text += "\bob"
>>> print(text)
Robert
>>> print(list(text))
['R', 'o', 'b', 'e', 'r', 't', '\x08', '\x08', '\x08', '\x08', 'p', '\x08', '\x08', 'u', '\x08', 'o', 'b']
Jammiejammin answered 26/2, 2020 at 15:56 Comment(0)
A
1

Sometimes your tcp-ip connection to a device that send data per-keystroke rather than the entire line when reaching a line-end. This happens a lot and and you still modernly see this, you actually would need to send backspaces to undo values already typed. And in many cases this is still useful and done. You need a command that means unsend-the-last-character. This is that character and if you need to use it. Use it.

Aspergillum answered 26/1, 2023 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.