[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: .format vs. %
From:       Terry Reedy <tjreedy () udel ! edu>
Date:       2011-12-31 21:09:09
Message-ID: jdntlu$o91$1 () dough ! gmane ! org
[Download RAW message or body]

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

-- 
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic