DEV Community

qing
qing

Posted on

3 Ways to Debug Python with f-strings

TIL: f-strings Support = for Instant Debug Output in Python 3.8+

When debugging Python code, it's often helpful to print out variable values. In Python 3.8 and later, f-strings provide a convenient way to do this with the = operator. By using f'{variable=}', you can print both the name and value of a variable in a single statement.

Before:

name = 'John'
age = 30
print(f'name: {name}, age: {age}')
Enter fullscreen mode Exit fullscreen mode

This approach requires manually typing out the variable name, which can be error-prone and tedious.

After:

name = 'John'
age = 30
print(f'{name=}, {age=}')
Enter fullscreen mode Exit fullscreen mode

Output:

name='John', age=30
Enter fullscreen mode Exit fullscreen mode

As you can see, using f'{variable=}' makes it easy to print out variable values with their corresponding names, making debugging much simpler.

By utilizing this feature, you can streamline your debugging process and reduce the amount of code you need to write, making it a valuable tool to have in your Python development toolkit.


Follow me on Dev.to for daily Python tips and quick guides!


喜欢这篇文章?关注获取更多Python自动化内容!

Top comments (0)