The backspace escape sequence \b
moves the cursor one position back, effectively deleting the character before it when printed in many consoles. It can be used to remove characters in strings during output.
The effect of \b
can vary depending on the terminal or console used. It works well in many standard command-line interfaces but might behave differently in some IDE output windows.
\b
# Example 1: Removing last character
print("Hello\b")
# Output:
# Hell
# Example 2: Removing multiple characters
print("Python\b\b\b")
# Output:
# Pytho
# Example 3: Backspace in the middle of string (overwrites previous char)
print("12345\b6")
# Output:
# 12346
# Example 4: Using backspace to erase and replace characters
print("ABCDE\b\bXY")
# Output:
# ABCXY
# Example 5: Backspace with multiple tabs and backspaces
print("A\tB\tC\b\bD")
# Output:
# A B DC