1

why print('abcd\b') is abcd Instead of abc

it is working if we put space after \b

>>> print('abcd\b')  
abcd
>>> print('abcd\b ') 
abc
Nikola
  • 411
  • 1
  • 5
  • 9

2 Answers2

1

\b is a backspace control character. Printing it moves the cursor back one space, just like on the typewriter. It does not delete anything. Unlike the typewriter, console output can only have one character in each position, so if you print one character over another, the old one is replaced by the new one.

So when you print "abcd\b", you print four characters, then backtrack, but you don't change anything else. When you print "abcd^b ", you print abcd, then return the cursor so it's over d, and replace the d with a space.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

The backspace doesn't delete anything, it moves the cursor to the left and it gets covered up by what you write afterward. You can refer to this question Backspace behavior in Python statement, what is correct behavior of printing a '\b' in code?

nilesh15
  • 35
  • 6