Adjusting Text: ljust(), rjust(), center(), zfill(), expandtabs()

The functions for adjusting text are as handy and convenient as the parsing functions. You'll use them a lot, particularly for attractive report printing.

The ljust(s, width) function left-justifies a string to a given width. The rjust(s, width) function right-justifies it. The center(s, width) function centers a string to a given width. Here are examples of all three:

>>> rjust("String",20)
'              String'

>>> rjust ("str",20)
'                 str'

>>> ljust("String",20)
'String              '

>>> ljust("str",20)
'str                 '

>>> center("str",20)
'        str         '

>>> center("String",20)
'       String       '

zfill(snum,width) pads a numeric string with leading zeros.

>>> zfill("0.1", 10)
'00000000.1'

expandtabs(s,tabsize) converts ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.