5

Why the result of print('helloo\b') is not hello and also result of print('heeee\b\b\blo') is not hello?

The answers are helloo and heloe.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • 3
    The behavior depends on what you use to view the result, such as your terminal or tools like less. Python has nothing to do with it. – MisterMiyagi Dec 07 '19 at 14:20
  • Note that `"hello!\b?" != "hello?"`, the `\b` has an effect during `print` but not on the content of the string. – norok2 Dec 07 '19 at 18:45

1 Answers1

4

Why the result of print('helloo\b') is not 'hello'

Because \b does not remove characters, it just moves the cursor back one position. Since there is nothing after the \b, nothing was overwritten. You end up with something like:

hello
    ^

Where ^ is the cursor.

and also result of print('heeee\b\b\blo') is not 'hello? the answers are 'helloo' and 'heloe'

Because in this you did move the cursor back 3 positions and wrote lo over the second and third es:

heeee
  ^

Becomes:

heloe

Note that all this is about printing the string somewhere like a terminal that recognizes the backspace character (given what you mention that you "see" in the question) -- the string in memory is still the same, including the \b characters. See e.g. The "backspace" escape character '\b': unexpected behavior? as well.

Acorn
  • 24,970
  • 5
  • 40
  • 69