Python 3.8 added an = specifier to f-strings. An f-string such as f'{expr=}' will expand to the text of the expression, an equal sign, then the representation of the evaluated expression.

Examples:


input:

1
2
3
4
from datetime import date
user = 'eric_idle'
member_since = date(1975, 7, 31)
f'{user=} {member_since=}'

output:

"user='eric_idle' member_since=datetime.date(1975, 7, 31)"

input:

1
2
delta = date.today() - member_since
f'{user=!s}  {delta.days=:,d}'

output (no quotes; commas):

'user=eric_idle  delta.days=16,075'

input:

1
2
3
from math import cos,radians
theta=30
print(f'{theta=}  {cos(radians(theta))=:.3f}')

output:

theta=30  cos(radians(theta))=0.866

Reddit discussion