[Tutor] String format question

Gonçalo Rodrigues op73418@mail.telepac.pt
Wed Nov 27 13:28:01 2002


----- Original Message ----- 
From: "andy surany" <mongo57a@comcast.net>
To: <tutor@python.org>


> Hello list,
> 
> I am concatenating a series of string elements for display to a scrolled
> list. The code is working fine - but the result is not as user friendly
> as I would like. This is what I'm doing:
> 
> # The individual contents of a,b, & c can be variable lengths.
> #
> # Now I use "map" to concatenate the individual records
> options=map((lambda i: a[i]+b[i]+c[i]),range(nrecs)
> self.ScrolledList(options) # This function populates the list.
> 
> This is what the output looks like:
> 
> aaa bbb ccc
> aaa bb ccccc
> a b c
> 
> Now what I would like is:
> 
> aaa  bbb  ccc
> aaa  bb    ccccc
> a      b     c
> 
> (i.e.: aligned columns)
> 
> How can/should I do this?
> 
> TIA.
> 
> -Andy
> 

Check out the string methods ljust, rjust, etc. For example:

>>> print str.ljust.__doc__
S.ljust(width) -> string

Return S left justified in a string of length width. Padding is
done using spaces.
>>> print 'aaa'.ljust(5) + 'bb'.ljust(5) + 'c'.ljust(5)
aaa  bb   c    

HTH,
G. Rodrigues