[Tutor] Numbers & Characters As Dictionary Keys

Alan G alan.gauld at freenet.co.uk
Tue Jun 21 18:47:41 CEST 2005

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

Its a standard string formatting operation in Python.
The % operator basically says substitute the values 
in the folowing tuple for the marked fields in the 
foregoing string.

The print statement above therefore is roughly equivalent 

print m + '\t' + menu[m][0]

But the formatting markers allow you to add formatting 
data like the minimum number of characters, right/left 
justification, number of digits after a decimal point, 
hex or decimal display of numbers etc etc.

Take a look in the Python docs for string formatting.

Alan G.

More information about the Tutor mailing list