How can named expressions be interpreted in f-strings?

Viewed 612

I am trying to use named expressions inside a f-string:

print(f"{(a:=5 + 6) = }")

Returns:

(a:=5 + 6) = 11

But I am hoping for something like this:

a = 11

Is that possible, by combining the walrus operator and f-strings (so that I don't have to declare the variable a first in a separate step)?

2 Answers

If you don't care about having a afterwards, you can print(f"{5+6}")

Otherwise, I don't think it's possible without some complex tricks

Related