[Tutor] Need to be taught a trick or two about printing in neat columns

Luke Paireepinart rabidpoobear at gmail.com
Thu Nov 2 10:45:23 CET 2006


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             


Notice how they're all lined up on the left (if the font in this e-mail 
is fixed-width.  Either way, the number of spaces for both columns is 
the same.)

ljust's functionality is essentially this:

def left_justify(astr,width):
    x = len(astr)
    while x < width:
        astr += ' '
        x += 1

HTH,
-Luke


More information about the Tutor mailing list