on str.format and f-strings

Meredith Montgomery mmontgomery at levado.to
Mon Sep 5 16:18:31 EDT 2022


ram at zedat.fu-berlin.de (Stefan Ram) writes:

> Meredith Montgomery <mmontgomery at levado.to> writes:
> ...
>>  d = { "name": "Meredith", "email": "mmontgomery at levado.to" }
>>  return "The name is {name} and the email is {email}".format(**d)
>>--8<---------------cut here---------------end--------------->8---
>>Is there a way to do this with f-strings?
>
>   I cannot think of anything shorter now than:
>
> eval( 'f"The name is {name} and the email is {email}"', d )
>
>   , but with the spaces removed, it's even one character
>   shorter than the format expression:
>
> eval('f"The name is {name} and the email is {email}"',d)
> "The name is {name} and the email is {email}".format(**d)
>
>   . 

Lol.  That's brilliant!  Thanks very much!


More information about the Python-list mailing list