3.8 f-string enhancements & debugging

I stumbled across this on Reddit earlier and thought it worth sharing here too.

Python 3.8 has enhanced f-string so you can now have self-documenting expressions. Why does this matter? Well if you are printing outputs to debug problems, (as Zed constantly reminds us to do…) then this can be very helpful…

Printing variables

name = "Paul"
age = 25

print(name)
print(age)

Output

Paul
25

Printing variables with f-strings

print(f"Name is {name}")
print(f"Age is {age}")

Output

Name is Paul
Age is 25

3.8 f-string enhancements

print(f"{name=}")
print(f"{age=}")

Output

name='Paul'
age=25
3 Likes

@gpkesley Thanks. This is helpful!

That’s a super odd “enhancement”. I guess it’s for debugging.

I think that’s why is slipped past everyone @zedshaw … with the typing in 3.6, breakpoint() in 3.7 and this in 3.8, does make you wonder what’s the thinking.

A free service run by Zed A. Shaw for learncodethehardway.org.