This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Backspace Escape Character at End of String
Type: behavior Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Jared Bevis, ezio.melotti, martin.panter, mrabarnett
Priority: normal Keywords:

Created on 2015-09-19 02:45 by Jared Bevis, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg251049 - (view) Author: Jared Bevis (Jared Bevis) Date: 2015-09-19 02:45
I'm a new python learner but I noticed inconsistent behavior of the backspace character when used in strings.  I'm running 2.7.10.  Whenever the backspace character '\b' is at the very end of a string, nothing happens, but if it is in the middle of string it behaves as expected.  

For example:
print "12345" + '\b'
returns:
>>>12345

But:
print "12345" + '\b' + '6'
returns:
>>> 12346
msg251051 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-09-19 04:00
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).

>>> s = "12345" + '\b' + '6'
>>> s
'12345\x086'
>>> s[5]  # ASCII code for backspace is 0x08
'\x08'

If you redirect the output to a file or other program, you will also see the backspace code is still there:

$ python2 -c 'print "12345" + "\b" + "6"' | hexdump -C
00000000  31 32 33 34 35 08 36 0a                           |12345.6.|
00000008
History
Date User Action Args
2022-04-11 14:58:21adminsetgithub: 69361
2015-09-19 04:00:33martin.pantersetstatus: open -> closed

nosy: + martin.panter
messages: + msg251051

components: - Regular Expressions
resolution: not a bug
2015-09-19 02:45:58Jared Beviscreate