Learn how to use Python f-strings with the = symbol and format specifiers to simplify debugging and output formatting.
= for Debugging + Format SpecifiersYou can now include the expression itself along with the value in the same f-string β very handy for debugging and clean output.
=name = "Yasir"
age = 30
print(f"{name=}, {age=}")
π€ Output:
name='Yasir', age=30
It shows both the variable name and its value. No need to manually write:
print("name =", name).
= and Format Specifierspi = 3.1415926535
print(f"{pi=:.2f}")
π€ Output:
pi=3.14
This uses
= for showing expressionand:.2ffor 2 decimal places formatting.
x = 5
y = 10
print(f"{x + y=}")
π€ Output:
x + y=15
Super useful when debugging calculations or complex logic.
| Feature | Syntax Example | Output | 
|---|---|---|
| Show name + value | f"{x=}" | x=5 | 
| With formatting | f"{x=:.2f}" | x=5.00 | 
| For expressions | f"{x + y=}" | x + y=15 | 
Absolutely! Hereβs a short quiz and practice code to reinforce your understanding of f-strings with = and format specifiers in Python.
Q1. What will the output be?
x = 5
print(f"{x=}")
A) x: 5 B) x=5 C) x D) 5
Q2. Which f-string correctly formats pi = 3.14159 to 2 decimal places and shows the expression?
A) f"{pi:.2f=}"
B) f"{=pi:.2f}"
C) f"{pi=:.2f}"
D) f"{pi:.2f}"
Q3. What does f"{x + y=}" display if x = 10 and y = 5?
A) x + y = 15 B) x+y=15 C) x + y=15 D) 15
Try running and modifying this code:
name = "Alice"
score = 87.456
passed = True
# Print with variable names using f-strings
print(f"{name=}")
print(f"{score=:.1f}")     # One decimal place
print(f"{passed=}")
# Debugging an expression
print(f"{score > 50=}")
print(f"{len(name)=}")
print(f"{score/10=:.2f}")
π Try This:
score or name.f"{name.upper()=}".print Functioninput() FunctionTutorials, Roadmaps, Bootcamps & Visualization Projects