on str.format and f-strings

Chris Angelico rosuav at gmail.com
Tue Sep 6 13:57:03 EDT 2022


On Wed, 7 Sept 2022 at 03:52, Meredith Montgomery <mmontgomery at levado.to> wrote:
>
> It seems to me that str.format is not completely made obsolete by the
> f-strings that appeared in Python 3.6.  But I'm not thinking that this
> was the objective of the introduction of f-strings: the PEP at
>
>   https://peps.python.org/pep-0498/#id11
>
> says so explicitly.

Precisely. It was never meant to obsolete str.format, and it does not.

> My question is whether f-strings can do the
> following nice thing with dictionaries that str.format can do:
>
> --8<---------------cut here---------------start------------->8---
> def f():
>   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?

No. That's not their job. That's str.format's job.

ChrisA


More information about the Python-list mailing list