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

Francis Avila francisgavila at yahoo.com
Wed Nov 19 23:55:00 CET 2003


John F Dutcher wrote in message
<2c82369d.0311191044.476a5a69 at posting.google.com>...

I have nothing to add to help you with your problem (aside from what Peter
already said), but just a tip:


>(If I don't import and use 'string.ljust), and use just 'ljust' I get
>a "Namerror'(Python 2.3.1)


You probably learned Python out of an old book, back when Python didn't have
string methods.  What I mean is, don't use the string module (it's slated
for depreciation), but just use ljust as a method of your string:

>>> ''.join(['a','b','c'])
'abc'
>>> 'a'.ljust(5)
'a    '
>>> '1234'.isdigit()
True
>>> dir('')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower',
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'replace', 'rfind', 'rindex', 'rjust', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

I'm sure you recognise many of those methods?  To learn more, try help('')
in an interactive session.

--
Francis Avila






More information about the Python-list mailing list