Vishal Gawde
Vishal Gawde

Reputation: 41

Escape Character \b in Python(3.4.2)

>>> print('x\b')    
x   
>>> print('x\ba')    
a    
>>> print('xyz\ba')    
xya

why doesn't it erase the character 'x' in first print statement?

Upvotes: 2

Views: 416

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476503

In short: \b is more a cursor one left than removing the previous one.

Backspace \b does not remove the character that is placed before the \b: it positions the cursor one back (given that is possible, otherwise nothing is done). Now if you then write other characters, the old character is overwritten. Compare it to an old typewriter with no correction tape. Or like this Wikipedia article says:

8 (backspace, BS, \b, ^H), used either to erase the last character printed or to overprint it.

Take for instance the following print statement:

>>> print('aaa\b\bb')
aba

What actually happens is (caret ^ shows the cursor positioning):

^

a
 ^

aa
  ^

aaa
   ^

aaa
  ^

aaa
 ^

aba
  ^

Upvotes: 4

Related Questions