d = { "name": "Meredith", "email": "mmontgomery@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?
Meredith Montgomery <mmontgomery@levado.to> writes:
...
d = { "name": "Meredith", "email": "mmontgomery@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)
.
On Monday, 5 September 2022 at 22:18:58 UTC+2, Meredith Montgomery wrote:
r...@zedat.fu-berlin.de (Stefan Ram) writes:
, 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!
Calling eval for that is like shooting a fly with a cannon.
Besides, this one is even shorter:
f"The name is {d['name']} and the email is {d['email']}"
r...@zedat.fu-berlin.de (Stefan Ram) writes:
, 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!
Julio Di Egidio <ju...@diegidio.name> writes:
On Monday, 5 September 2022 at 22:18:58 UTC+2, Meredith Montgomery wrote:
r...@zedat.fu-berlin.de (Stefan Ram) writes:
, 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!
Calling eval for that is like shooting a fly with a cannon.
Indeed! But we're not looking for production-quality code. Just an
extreme way to satisfy a silly requirement.
Besides, this one is even shorter:
f"The name is {d['name']} and the email is {d['email']}"
This doesn't quite satisfy the requeriments. We're trying to specify
only the keys, not the dictionary. (But maybe the requirements did not
say that explicitly. I'd have to look it up again --- it's been
snipped. It's not important. Thanks much for your thoughts!)
On Tuesday, 6 September 2022 at 01:03:02 UTC+2, Meredith Montgomery wrote:
Julio Di Egidio <ju...@diegidio.name> writes:
On Monday, 5 September 2022 at 22:18:58 UTC+2, Meredith Montgomery wrote: >> >> r...@zedat.fu-berlin.de (Stefan Ram) writes:
, 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!
Calling eval for that is like shooting a fly with a cannon.
Indeed! But we're not looking for production-quality code. Just an
extreme way to satisfy a silly requirement.
Indeed, as far as programming goes, even the premise is
totally nonsensical. Maybe you too better go to the pub?
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.
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@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?
On Wed, 7 Sept 2022 at 03:52, Meredith Montgomery <mmontgomery@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@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.
On Wed, 7 Sept 2022 at 03:52, Meredith Montgomery <mmont...@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": "mmont...@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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 71:26:30 |
Calls: | 6,712 |
Files: | 12,244 |
Messages: | 5,356,970 |