Skip to content Skip to sidebar Skip to footer

Python Strings: Backspace At The End Of String Behaves Differently

I'm experimenting with the backspace character \b in Python strings. I tried this: >>> s = 'The dog\b barks' >>> print(s) The do barks This behaves as expected.

Solution 1:

Python doesn't treat the backspace (\b) character specially, but Windows command prompt (cmd.exe) treats the backspace by moving the cursor left one character, then printing the rest of the string. Because there is nothing left to print, moving the cursor left doesn't actually do anything.


Solution 2:

That isn't a behavior of python but your output StreamHandler. The \b doesn't cancel the character before it, it instructs your output handler device (terminal/console) to move the cursor backward.

What basically happens is, that you are placing the cursor one character to the left and continue streaming what is following. It visually look like you are putting a space as the next following character is aspace.

In Both Unix based and Windows the output behavior is the same:

Unix test / Mac Terminal:

echo -e "Hello test\b" 
>Hello test

Windows test / CMD / Powershell :

echo "Hello test`b"
>Hello test

Post a Comment for "Python Strings: Backspace At The End Of String Behaves Differently"