0
Community
  • 1
  • 1
Teena Joy
  • 1
  • 3
  • 2
    Does this answer your question? [The "backspace" escape character '\b': unexpected behavior?](https://stackoverflow.com/questions/6792812/the-backspace-escape-character-b-unexpected-behavior) – avayert Feb 18 '20 at 10:21
  • 1
    Can you clarify why think ``\b`` did not work? How does your *actual* output differ from the *expected* output? How you you check whether ``\b`` is working -- are you aware that it is usually not visible? – MisterMiyagi Feb 18 '20 at 10:36

1 Answers1

1

This is got to do with how the terminal interprets escape characters. It had been reported for Python 2 a few years back. Quoting:

Python itself doesn’t treat backspace specially. What you are probably seeing is the terminal interpreting the backspace specially by moving the cursor left (without erasing anything).

This means that, if you have a character after \b, the terminal will move the cursor left and "overwrite" the original character with the new one. If there are no other characters, nothing will overwrite the old character.

Trying piping the output to hexdump using your example shows:

$ python3.8 -c 
'print("abc#\bde#\bf#\bghi#\bjklmn#\bop\b")' | hexdump -C
00000000  61 62 63 23 08 64 65 23  08 66 23 08 67 68 69 23  |abc#.de#.f#.ghi#|
00000010  08 6a 6b 6c 6d 6e 23 08  6f 70 08 0a              |.jklmn#.op..|
0000001c

You can notice the 08 character there (\b).

Jongware
  • 22,200
  • 8
  • 54
  • 100
Jir
  • 2,985
  • 8
  • 44
  • 66