2 ms·
You can already almost do this in Python, with the native string format operation: >>> name = "Foo Bar" >>> age = 25 >>> "Hi, my name is {name} and I'm {age}
by ForHackernews 12y ago
You can already almost do this in Python, with the native string format operation:
>>> name = "Foo Bar"
>>> age = 25
>>> "Hi, my name is {name} and I'm {age} years old.".format(splatlocals())
"Hi, my name is Foo Bar and I'm 25 years old."
Arguably, this an abuse of `locals()`, but it gets you very nearly the same kind of use-variables-in-strings-with-curly-braces functionality.
Edit: HN markdown doesn't seem to let you escape the star italics operator. To be clear, you have to double-star splat locals().
- maxerickson 12y agoRepeating my similar comment, str.format_map takes a dictionary directly (available since 3.2).