[Python-ideas] String formatting

Steven D'Aprano steve at pearwood.info
Sat Sep 28 14:32:46 CEST 2013


On Sat, Sep 28, 2013 at 01:53:37AM -0700, Ram Rachum wrote:
> Any reason why string formatting using % doesn't work when the list of 
> arguments is in a list rather than a tuple?

Because it's not supposed to. It is part of the design of % that 
arbitrary objects, including lists, require only a single % target:

py> L = list(range(8))
py> "Values: %s" % L
'Values: [0, 1, 2, 3, 4, 5, 6, 7]'

The deliberately single exception to that are tuples. This is 
unavoidable, since there's otherwise no other way for a binary operator 
like % to take arbitrary numbers of arguments.

Besides, even if this were a good idea, 20+ years of code that expects 
lists to be treated as a single object for the purposes of % formatting 
says we can't change it now.


-- 
Steven


More information about the Python-ideas mailing list