.format vs. %

Terry Reedy tjreedy at udel.edu
Sat Dec 31 16:09:09 EST 2011


On 12/31/2011 2:24 PM, Tim Chase wrote:
> On 12/31/11 12:57, Benjamin Kaplan wrote:
>> format is a method of the string class. You store the string the same way
>> you would any other.
>>
>> formatter = "Hello, {}"
>> print(formatter.format("world"))
>
> Just to note that this syntax doesn't quite work in some earlier
> versions (tested below in 2.6, which came stock in Debian Stable)

Autonumbering of {} was introduced, at my suggestion, in 3.1 and 
included with 2.7 (I presume).
>
>  >>> formatter = "Hello, {}"
>  >>> print(formatter.format("world"))
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: zero length field name in format
>
>
> It needs to be spelled "{0}"
>
> -tkc
>
>


-- 
Terry Jan Reedy




More information about the Python-list mailing list