.format vs. %

Tim Chase python.list at tim.thechases.com
Sat Dec 31 14:24:10 EST 2011


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)

 >>> 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





More information about the Python-list mailing list