[Tutor] Need to be taught a trick or two about printing in neat columns
Dick Moores
rdm at rcblue.com
Thu Nov 2 12:31:18 CET 2006
At 03:13 AM 11/2/2006, Kent Johnson wrote:
>Luke Paireepinart wrote:
> > Instead of helping you with your specific problem, I'll give you this
> > information and see what you can make of it.
> >
> > >>> print 'a'.ljust(20)+'b'.ljust(20)
> > a b
> > >>> print 'carrah'.ljust(20)+'foobar'.ljust(20)
> > carrah foobar
>
>Another way to do this is with string formatting, I think it is a more
>readable and flexible solution:
>
>In [1]: print '%-20s %-20s' % ('a', 'b')
>a b
>
>In [2]: print '%-20s %-20s' % ('carrah', 'foobar')
>carrah foobar
>
>See this page for details:
>http://docs.python.org/lib/typesseq-strings.html
Thanks, Kent. I agree. So now that function has become
def printListOfAllUnitsAndAbbreviations():
"""
Prints a 3-column list of all units and their abbreviations,
but not their categories.
"""
lstAll = allUnitsAndTheirAbbreviationsAndCategories()
for i in range(0, len(lstAll)-1 ,3):
print '%-27s %-27s %-27s' % (lstAll[i][2:],
lstAll[i+1][2:], lstAll[i+2][2:])
print
More information about the Tutor
mailing list