What is the use of '\b' (backspace)

笑着哭i 提交于 2020-01-23 08:30:09

问题


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.


回答1:


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 BS ` (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.




回答2:


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)




回答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.



来源:https://stackoverflow.com/questions/48568885/what-is-the-use-of-b-backspace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!