I am working on python escape characters. I found that \b is not working for the last indexed character of the string in Python 3.8.Why is it so?
Input :
print("abc#\bde#\bf#\bghi#\bjklmn#\bop\b#\b")
Output :
abcdefghijklmno#
Asked
Active
Viewed 241 times
0
-
2Does 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
-
1Can 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 Answers
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
).