Conduit
Conduit

Reputation: 2735

"\b" acting improperly in string

I have a small bit of code that seems to be acting improperly; I'm new to Python, and imagine I'm overlooking something. The code is:

bs = ("\b", "\b", "\b", "\b", "\b", "\b", "\b", "\b", "\b");
print "b%ra%rc%rk%rs%rl%ra%rs%rh%r" % bs;
print "b%sa%sc%sk%ss%sl%sa%ss%sh%s" % bs;

When I run this in Powershell the output is:

b\x08a\x08c\x08k\x08s\x08l\x08a\x08s\x08h\x08
h

Why does the last h\b evaluate to h when using string (%s) output, when it seems like it should be deleted from the raw (%r) output?

Upvotes: 1

Views: 2245

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121216

You are not erasing the character with a \b; you are only backing up one position.

So the characters are overwritten by the next printed character instead. Add a space and it works:

>>> print "b%sa%sc%sk%ss%sl%sa%ss%sh%s " % bs

Upvotes: 1

Related Questions