PEP proposal: enhanced string functions

Stephen Ferg steve at ferg.org
Tue Nov 13 21:54:47 CET 2001


I have been thinking about two proposals for PEPs, and I would like to see what 
other folks think of them. They aren't anything major, just small enhancements 
that I think would be easy to do and useful.

-- Steve Ferg (steve at ferg.org)

=========================================================
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.


Rationale

    The rational for this PEP is that it will add some minor, but handy, ease-
    of-use functionality to the affected functions.  It will not break any
    existing code, and it should be easy to implement.

    This functionality will make it easier to do some simple but useful
    string formatting tasks.

    - It can be used to create leaders.  For example:

            import string
            budget_items = [("Cookies", "13.25"), ("Sodas", "8.50")]
            # print budget items with leaders
            for item, expenditure in budget_items:
                s1 = string.ljust(item, 15, ".")
                s2 = string.rjust(expenditure, 10, ".")
                print s1 + s2

      produces:

          Cookies.............13.25
          Sodas................8.50

   - It can be used to create headings:

          s = "Chapter 2: Working with XML"
          print s.center(50, "-")

      produces

          ------------Chapter 2: Working with XML-----------

=========================================================
PEP: XXX2
Title: Optional argument for string strip functions

Abstract

    This PEP proposes a minor enhancement to the lstrip(), rstrip(), and strip()
    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 "stripchar".  If the stripchar argument is unspecified or is
    specified as None, then these functions and methods will do exactly what
    they now do, and strip whitespace. If the stripchar argument is specified,
    and not specified as None, it must contain a single character. If the
    stripchar argument is specified, then rather than stripping out whitespace,
    the functions and methods will strip out the specified stripchar character.


Rationale

    The rational for this PEP is that it will add some minor, but handy, ease-
    of-use functionality to the affected functions.  It will not break any
    existing code, and it should be easy to implement.

    This functionality will make it easier to do some simple but useful
    string formatting tasks.

    - It can be used to strip out leaders.  For example:

            s = "Chapter 1........................"
            print s.rstrip(".")

      produces:

            Chapter 1

    - Its most frequent use will probably be be to assist in cleaning up files
    by cleaning out junk characters.  For example:

          s = "?????????????Text that I want?????????"
          s = s.strip("?")

      produces a value for s of "Text that I want".



More information about the Python-list mailing list