0

Following python code is not giving me a backspace as it's supposed to.

var=('hel\b')
print(var)

Outputs:

hel
>>>

But setting terminal character as nothing works as expected.

var=('hel\b')
print(var,end='')

Outputs:

he>>>

In following example.

var=('hello\b\b \b')
print(var)

Outputs:

hel o
>>>

What exactly is happening here ?

Am coding in Python 3.5 in Notepad++. Os is Windows 7, 32-bit.

cs95
  • 379,657
  • 97
  • 704
  • 746
Rahul
  • 2,658
  • 12
  • 28
  • Backspaces are *never* carried over into next lines. A newline always moves the cursor into the next line and sets it back to the beginning of the line. – poke Jan 30 '18 at 12:48
  • @poke That doesn't look like the case. – iBug Jan 30 '18 at 12:48
  • @iBug … because? – poke Jan 30 '18 at 12:49
  • It's a python question. Why mark it duplicate with C question ? There could be different details even though previous question explains backspace behavior in C. – Rahul Jan 30 '18 at 12:51
  • 1
    Because the behavior of the `\b` backspace sequence is the same. The explanations in the answers there (which have nothing to do with C btw) work in the same way. – *“There could be different details”*, but there aren’t. In the same way this question was not specific to Python *3*. – poke Jan 30 '18 at 12:52

1 Answers1

2

The backspace character \b only moves the cursor one character backwards, but never deletes or overwrites anything.

In your second example, you should be able to observe it precisely: the Python prompt >>> has overwritten the last character, l.

If you had written print('hel', end=''), you would have seen:

hel>>>

The \b character shifted the cursor one character backwards, which made the prompt started printing at l, instead of after l.

If you want it to erase a character, fill it with a space, then backshift again:

>>> print('hel\b \b')
he
>>>
iBug
  • 35,554
  • 7
  • 89
  • 134