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

Fredrik Lundh fredrik at pythonware.com
Wed Nov 19 09:23:16 EST 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))
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))
20

</F>








More information about the Python-list mailing list