Pre-PEP: Refusing to guess in string formatting operations
Greg Ewing (using news.cis.dfn.de)
me at privacy.net
Wed Mar 12 19:58:45 EST 2003
Beni Cherniavsky wrote:
> The `string formatting operation`_ - ``format % values`` - has a wart.
> In the face of non-tuple values, it assumes a singleton tuple around
> it.
I don't think I can remember ever being tripped up
by this. I don't see it as enough of a problem to
be worth changing anything -- particularly not as
drastic as adding a new operator or deprecating
any existing one.
If you find it a problem, you can easily write a
small function which behaves the way you want:
>>> def fmt(s, *a):
... return s % a
...
>>> fmt("abc %s def", 1)
'abc 1 def'
>>> fmt("abc %s def", (1, 2))
'abc (1, 2) def'
>>> fmt("abc %s def %s ghi", 1, 2)
'abc 1 def 2 ghi'
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
More information about the Python-list
mailing list