Using 'string.ljust' to try and hold a fixed width.......

Fredrik Lundh fredrik at
Wed Nov 19 15:23:16 CET 2003

John F Dutcher wrote:

> The spaces expected are 'NOT' being provided with 'string.ljust'....
> If I simply print the field immediately as in:
>        print string.ljust(form.getfirst("lname",' '),15)
> they are not present; they are not present when assigned to the list,
> and, of course, they are not present in the final string.

are you sure they're not there, or is the problem just that print
doesn't show them to you?  try using "repr" or "len":

>>> import string
>>> print repr(string.ljust("field", 15))
'field          '
>>> print len(string.ljust("field", 15))

also note that ljust doesn't do anything if the input string is
longer than the field width:

>>> print len(string.ljust("fieldfieldfieldfield", 15))


More information about the Python-list mailing list