[Tutor] Dictionaries

Alan Gauld alan.gauld at freenet.co.uk
Thu Jan 26 09:05:38 CET 2006


> Is there anyway to print informtation from dictionaries better than this?:

For formatted printing look at the print format operator '%'

You can specify field sizes, justification etc

thus : 

>>> pairs = {"Jon Moore": "Tony Moore",
         "Simon Nightingale": "John Nightingale",
         "David Willett": "Bernard Willet",
         "John Jackson": "Stuart Jackson",
         "James Southey": "Richard Southey",
         "William Forsythe": "Shaun Forsythe"}
>>> print pairs.keys()
['David Willett', 'Jon Moore', 'John Jackson', 'Simon Nightingale', 'James
Southey', 'William Forsythe']

>>> for father,son in pairs.items:
...       print "%20s\t%20s" % (father,son)

Which will print father and son each in a field 20 characters wide 
with a tab between them.

The docs on the format operator are here:

http://docs.python.org/lib/typesseq-strings.html

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list