In Python 3.8 and later, placing an equal sign after a variable inside an f-string automatically prints both the variable’s name and its current value for easy debugging.
Code:
name = “Ravi”
age = 22
city = “Delhi”
# Quick debug trick
print(f”{name=}”)
print(f”{age=}”)
print(f”{city=}”)
Output:
name=’Ravi’
age=22
city=’Delhi’
Quick answers:
Q: Which Python version supports the equal sign in f-strings?
A: This feature was introduced in Python 3.8, so any version 3.8 or newer supports it.
Q: Does this equal sign trick work with expressions?
A: Yes, you can place expressions like f'{2 + 2=}’ and Python will output both the expression and its result.
Q: Why is this better than normal print statements?
A: It eliminates the need to manually type the variable name as text inside the print function, saving time and avoiding typos.
PythonWithRavi: Python tricks, mini projects and interview questions – new Shorts every day.
The code in this video was run and the output shown is the real output.
Script written with AI assistance; the voice is synthetic.
#Shorts #Python #PythonFStringDebug #PythonDebugTrick #Coding
source

Did you already know about this f-string equal sign trick? 🐍