Most Python escape characters are easy to understand:
\n # New line
\t # Tab
You see them once, and their behavior immediately makes sense.
But then there are these two mysterious escape characters:
\b # Backspace
\r # Carriage Return
And suddenly Python outputs start behaving strangely.
Characters don’t disappear properly.
Text gets overwritten unexpectedly.
Different terminals show different results.
If you've ever been confused by them, you're not alone.
The reason is simple:
\band\rare not text-formatting characters.
They are cursor-control characters.
And once you understand cursor movement, everything starts making sense.
First, What Are Escape Characters?
Escape characters begin with a backslash \.
They tell Python:
“Perform a special action instead of printing normal text.”
Example:
print("Hello\nWorld")
Output:
Hello
World
Here, \n creates a new line.
The Real Meaning of \b
Most beginners think:
\b means:
“Delete the previous character.”
But technically, that’s NOT what happens.
\b means:
Move the cursor one position backward.
That’s it.
Deletion only becomes visible if something overwrites that position afterward.
Example 1
print("Helloo\b")
Many people expect: Hello
But sometimes the output still appears as: Helloo
Why?
Because the cursor moved backward…
…but nothing replaced the last character.
Visual Understanding
Initially: Helloo
After \b: Helloo
Cursor moved back one step.
But the text itself still exists.
Example 2 — Now It Makes Sense
print("Helloo\bWorld")
Output:
HelloWorld
What happened?
Step-by-step:
First:
Helloo
Then \b moves cursor back:
Helloo
^
Now "World" starts printing from that position:
HelloWorld
The extra o gets overwritten by W.
The Real Meaning of \r
\r stands for:
Carriage Return
It moves the cursor back to the beginning of the current line.
Anything printed afterward starts overwriting from the start.
Example
print("Hello\rHi")
Output:
Hillo
Why?
Process:
First:
Hello
Then \r moves cursor to the beginning:
Hello
^
Now "Hi" overwrites the first two letters:
Hillo
The remaining characters (llo) stay because they were never overwritten.
A More Complex Example
A More Complex Example
txt = "We are the so-called \rVikings\r from the north"
print(txt)
Possible output:
from the north-called
Looks strange at first.
But internally:
Original text gets printed
\r resets cursor to start
"Vikings" overwrites beginning
Another \r
" from the north" overwrites again
Only the overwritten part changes.
The remaining old characters stay.
Real-World Usage of \r
\r is heavily used in:
- progress bars
- loading animations
- terminal status updates
Example:
import time
for i in range(5):
print(f"\rLoading {i}", end="")
time.sleep(1)
Instead of printing multiple lines, the same line updates repeatedly.
Why Outputs Sometimes Differ
One confusing part is:
Different terminals handle
\band\rdifferently.
Some terminals visually erase characters.
Some only move the cursor.
Some IDE consoles behave differently from real terminals.
That’s why outputs can vary slightly.
The Key Insight
The biggest misunderstanding is thinking:
\b means “delete”.
and
\r means “replace”.
They actually mean:
-
\b→ move cursor backward -
\r→ move cursor to line start
Once you understand cursor movement, both escape characters become very predictable.
Final Thoughts
Tiny concepts like these reveal how much is happening behind simple console outputs.
Most developers use escape characters daily…
…but very few actually understand what’s happening internally.
And honestly, that’s what makes programming fun
Top comments (0)