string interpolation for python
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Apr 2 14:56:49 EDT 2012
On Tue, 03 Apr 2012 00:58:38 +1000, Chris Angelico wrote:
> Maybe. But it (percent-notation) is expressive and insanely powerful.
> Moreover, it obeys the rule that you pay for the complexity you use, no
> more and no less. (Although I think there's one lack in Python's
> implementation - I can't find a way to use an argument more than once,
> without switching to "dictionary mode" and using keys for everything. I
> can't, for instance, use "Hello hello, %s %[0]s!"%name to use the name
> twice. But since that isn't in the original C implementation, it's
> hardly mandatory.)
Ack.
In this case, you can use format:
>>> "Hello {0}, what's up with {1}? Hey, {0} I'm speaking to you!".format
('George', 'Melissa')
"Hello George, what's up with Melissa? Hey, George I'm speaking to you!"
--
Steven
More information about the Python-list
mailing list