Yet another Python textbook

Terry Reedy tjreedy at udel.edu
Thu Nov 22 05:04:26 CET 2012


On 11/21/2012 6:21 PM, Joshua Landau wrote:

> Since we've decided to derail the conversation...
>
> "{}".format() is a blessing an "" % () should go. "%" has no relevance
> to strings, is hard to "get" and has an appalling* syntax. Having two
> syntaxes just makes things less obvious, and the right choice rarer.
>
> str.format is also really easy. I don't understand what makes you disagree.
>
> Easy vs easier:
>
>  >>> "%s %s %s" % (1, 2, 3)
> '1 2 3'
>
>  >>> "{} {} {}".format(1, 2, 3)
> '1 2 3'

You forgot the cases where % formatting has to be special cased.
 >>> "The answer is {}.".format(1)
'The answer is 1.'
 >>> "The answer is {}.".format((1,))
'The answer is (1,).'
 >>> "The answer is %s" % 1
'The answer is 1'
 >>> "The anwser is %s" % (1,)
'The answer is 1'
 >>> "The answer is %s" % ((1,),)
'The answer is (1,)'


-- 
Terry Jan Reedy



More information about the Python-list mailing list