[Tutor] Numbers & Characters As Dictionary Keys

Brian van den Broek bvande at po-box.mcgill.ca
Mon Jun 20 21:05:28 CEST 2005

Don Parris said unto the world upon 20/06/2005 14:10:
> On Mon, 20 Jun 2005 09:11:53 +0100
> "Alan G" <alan.gauld at freenet.co.uk> wrote:


>>for m in menu.keys():
>>   print "%s\t%s" % (m,menu[m][0])
> I am curious what the "%" by itself is doing.


> Don

Hi Don,

it is indicating that the tuple that follows is the source that the 
values for the '%s's in "%s\t%s" will be drawn from. I generally write 
it without the space between the '%' and the tuple; I don't know what 
the general practise is.

 >>> print "%s won't %s" ('this', 'work')
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
TypeError: 'str' object is not callable
 >>> print "%s will %s" %('this', 'work')
this will work
 >>> print "%s will %s" % ('this', 'work, too')
this will work, too


Brian vdB

More information about the Tutor mailing list