[Python-ideas] String formatting

Terry Reedy tjreedy at udel.edu
Sat Sep 28 22:34:03 CEST 2013


On 9/28/2013 4:53 AM, 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 that would double the troublesome anomaly of a tuple being 
treated as a sequence of objects and not just an object itself.

 >>> 'object %s' % [1,2]
'object [1, 2]'
 >>> 'object %s' % (1,2)
Traceback (most recent call last):
   File "<pyshell#1>", line 1, in <module>
     'object %s' % (1,2)
TypeError: not all arguments converted during string formatting

One of the reasons for .format() is to eliminate that anomaly.

 >>> 'object {}'.format([1,2])
'object [1, 2]'
 >>> 'object {}'.format((1,2))
'object (1, 2)'

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list