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

Kent Johnson kent37 at tds.net
Thu Nov 2 12:13:58 CET 2006

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:


More information about the Tutor mailing list