PEP proposal: enhanced string functions

Eddie Corns eddie at holyrood.ed.ac.uk
Fri Nov 16 19:56:57 CET 2001


>PEP: XXX1
>Title: Optional pad character for string justification functions

>Abstract

>    This PEP proposes a minor enhancement to the ljust(), rjust(), and center()
>    functions in the standard string module, and their corresponding methods for
>    string objects.

>    The proposal is to allow these functions and methods to take one optional
>    argument in addition to the arguments that they now accept.  The argument is
>    called "pad", and it will default to a single space character.  The pad
>    argument may be over-ridden only by an argument that consists of a single
>    character. If the pad argument is over-ridden, then the specified pad
>    character, rather than the default space character, will be used in padding
>    the string to the desired length.


If you were going to improve justification, I suggest the pad should be a
string of any length and there should be another optional argument for what
direction the string is padded from (ie either from what we have so far to the
specified limit or backwards from the limit to where we are).  Eg

print "Chapter 1".rjust(65,"  .")+' Page 1'
print "Chapter 1.2".rjust(65,"  .")+' Page 3'

which produces:

Chapter 1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .   Page 1
Chapter 1.2  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . Page 3

OR much better (for this case)

print "Chapter 1".rjust(65,"  .",true)+' Page 1'
print "Chapter 1.2".rjust(65,"  .",true)+' Page 3'

which produces:

Chapter 1 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . Page 1
Chapter 1.2  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . Page 3

Certainly I was looking for something just like this recently.

However I can't help thinking that some more general purpose formatting tools
are what's really needed.  I was a little bit surprised at the lack of
formatting functions in Python.  Maybe, I'll write some.

Eddie



More information about the Python-list mailing list