DEV Community

Toolloom
Toolloom

Posted on • Originally published at toolloom.com on

Stop Writing print('var =', var) in Python

We've all been there: quickly sprinkling print statements throughout our code to see what a variable holds during runtime. But writing 'print(f"user_id: {user_id}")' over and over gets tedious. Next time, try the f-string shortcut by adding an equals sign after the variable name: 'print(f"{user_id=}")'.

This will automatically output both the variable name and its value (for example, 'user_id=42'), saving you keystrokes and making your quick-and-dirty debugging sessions much faster. It even works with expressions, like 'print(f"{len(users)=}")'!

Top comments (0)