Learn with Yasir

Share Your Feedback

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 expression and :.2f for 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 score or name.
  • Add more expressions like f"{name.upper()=}".

  • 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 print Function
  • input() Function

🧠 Practice & Progress

Explore More Topics

πŸ“˜ Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules