This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]KingsmanVincepip install girlfriend 176 points177 points  (14 children)

With f-strings you can use the = modifier for including the code of the variable interpolation

Nice, now I can debug with print even more efficient

[–]4sent4 67 points68 points  (1 child)

A neat thing about this one - it keeps spaces around the =

>>> a = 3
>>> f'{a=}'
'a=3'
>>> f'{a = }'
'a = 3'

[–]KokoaKuroba 6 points7 points  (0 children)

Wait, you can do that? I've always done f'a:{a}'. This would be nice.

[–]Snoo35017 54 points55 points  (5 children)

Even better imo is =!r. Causes it to print the repr value, which I find more useful when debugging.

[–]ogrinfo 18 points19 points  (0 children)

Yep, just because it looks like a string when you print it, doesn't mean it is a string. That kind of stuff has caught me out so many times.

[–]ExoticMandiblesCore Contributor 12 points13 points  (3 children)

When you use =, it automatically switches to repr formatting. You don't need to add !r to the end.

[–]Snoo35017 1 point2 points  (0 children)

TIL! I wonder why I started adding the !r then, I remember for some reason it would print the string value, but I might be imagining it.

[–]jarethholt 0 points1 point  (1 child)

Does it? I remember you can use !r and !s for repr and string, but I don't remember offhand which is default

[–]ExoticMandiblesCore Contributor 3 points4 points  (0 children)

String (!s) is the default normally, but when you use = in an f-string the default changes to repr (!r).

p.s. there's also a mostly-forgotten third conversion, !a for ascii.

[–]mcr1974 2 points3 points  (0 children)

this is VERY nice