1

So from what I understand. \b removes the previous character in the code. So I understand why my first line of code is cat. But what is going on after that?

print("car\bt")
print("car\b\bt")
print("car\b\b\bt")
print("car\b\b\b\bt")
print("car\b\b\b\b\bt")
print("car\b\b\b\b\b\bt")
print("car\b\b\b\b\b\b\bt")
Output:
cat
ctr
tar
tar
tar
tar
tar

2 Answers2

0

A single backspace moves the "cursor" position one position back (to the left). Two backspaces move it two positions back. Since your string, "car", has only three letters, three or more backspaces move it exactly three positions to the left, making "tar" out of "car".

Igor F.
  • 2,649
  • 2
  • 31
  • 39
0

A backspace character moves the cursor backwards, but unlike the Backspace keyboard key it doesn't actually erase anything. It merely moves the cursor. It's more like pressing than Backspace.

If you want to erase a character then a common technique is to print \b \b: move the cursor left, overwrite with a space, then move it left again.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578