Python f-Strings with = and Format Specifiers (Debugging Made Easy)
Learn how to use Python f-strings with the = symbol and format specifiers to simplify debugging and output formatting.
โ
= for Debugging + Format Specifiers
You can now include the expression itself along with the value in the same f-string โ very handy for debugging and clean output.
๐งช Example 1: Expression with =
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).
๐งช Example 2: Expression with = and Format Specifiers
pi = 3.1415926535
print(f"{pi=:.2f}")๐ค Output:
pi=3.14
This uses
= for showing expressionand:.2ffor 2 decimal places formatting.
๐งช Example 3: Debug an Expression
x = 5
y = 10
print(f"{x + y=}")๐ค Output:
x + y=15
Super useful when debugging calculations or complex logic.
โ Summary
| 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.
๐ง Quick Quiz (Multiple Choice)
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
Task: ๐งช Practice Code
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:
- Change values of
scoreorname. - Add more expressions like
f"{name.upper()=}".
๐ Related Topics
- F-Strings in Python โ F-strings (formatted string literals) are a feature introduced in Python 3.6 that provide a concise and readable way to embed expressions inside string literals. ๐ Learn more
- Python
printFunction input()Function