home *** CD-ROM | disk | FTP | other *** search
- <TITLE>string -- Python library reference</TITLE>
- Next: <A HREF="../r/regex" TYPE="Next">regex</A>
- Prev: <A HREF="../s/string_services" TYPE="Prev">String Services</A>
- Up: <A HREF="../s/string_services" TYPE="Up">String Services</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>4.1. Standard Module <CODE>string</CODE></H1>
- This module defines some constants useful for checking character
- classes and some useful string functions. See the modules
- <CODE>regex</CODE> and <CODE>regsub</CODE> for string functions based on regular
- expressions.
- <P>
- The constants defined in this module are are:
- <P>
- <DL><DT><B>digits</B> -- data of module string<DD>
- The string <CODE>'0123456789'</CODE>.
- </DL>
- <DL><DT><B>hexdigits</B> -- data of module string<DD>
- The string <CODE>'0123456789abcdefABCDEF'</CODE>.
- </DL>
- <DL><DT><B>letters</B> -- data of module string<DD>
- The concatenation of the strings <CODE>lowercase</CODE> and
- <CODE>uppercase</CODE> described below.
- </DL>
- <DL><DT><B>lowercase</B> -- data of module string<DD>
- A string containing all the characters that are considered lowercase
- letters. On most systems this is the string
- <CODE>'abcdefghijklmnopqrstuvwxyz'</CODE>. Do not change its definition ---
- the effect on the routines <CODE>upper</CODE> and <CODE>swapcase</CODE> is
- undefined.
- </DL>
- <DL><DT><B>octdigits</B> -- data of module string<DD>
- The string <CODE>'01234567'</CODE>.
- </DL>
- <DL><DT><B>uppercase</B> -- data of module string<DD>
- A string containing all the characters that are considered uppercase
- letters. On most systems this is the string
- <CODE>'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</CODE>. Do not change its definition ---
- the effect on the routines <CODE>lower</CODE> and <CODE>swapcase</CODE> is
- undefined.
- </DL>
- <DL><DT><B>whitespace</B> -- data of module string<DD>
- A string containing all characters that are considered whitespace.
- On most systems this includes the characters space, tab, linefeed,
- return, formfeed, and vertical tab. Do not change its definition ---
- the effect on the routines <CODE>strip</CODE> and <CODE>split</CODE> is
- undefined.
- </DL>
- The functions defined in this module are:
- <P>
- <DL><DT><B>atof</B> (<VAR>s</VAR>) -- function of module string<DD>
- Convert a string to a floating point number. The string must have
- the standard syntax for a floating point literal in Python, optionally
- preceded by a sign (`<SAMP>+</SAMP>' or `<SAMP>-</SAMP>').
- </DL>
- <DL><DT><B>atoi</B> (<VAR>s</VAR>[, <VAR>base</VAR>]) -- function of module string<DD>
- Convert string <VAR>s</VAR> to an integer in the given <VAR>base</VAR>. The
- string must consist of one or more digits, optionally preceded by a
- sign (`<SAMP>+</SAMP>' or `<SAMP>-</SAMP>'). The <VAR>base</VAR> defaults to 10. If it is
- 0, a default base is chosen depending on the leading characters of the
- string (after stripping the sign): `<SAMP>0x</SAMP>' or `<SAMP>0X</SAMP>' means 16,
- `<SAMP>0</SAMP>' means 8, anything else means 10. If <VAR>base</VAR> is 16, a
- leading `<SAMP>0x</SAMP>' or `<SAMP>0X</SAMP>' is always accepted. (Note: for a more
- flexible interpretation of numeric literals, use the built-in function
- <CODE>eval()</CODE>.)
- </DL>
- <DL><DT><B>atol</B> (<VAR>s</VAR>[, <VAR>base</VAR>]) -- function of module string<DD>
- Convert string <VAR>s</VAR> to a long integer in the given <VAR>base</VAR>. The
- string must consist of one or more digits, optionally preceded by a
- sign (`<SAMP>+</SAMP>' or `<SAMP>-</SAMP>'). The <VAR>base</VAR> argument has the same
- meaning as for <CODE>atoi()</CODE>. A trailing `<SAMP>l</SAMP>' or `<SAMP>L</SAMP>' is not
- allowed, except if the base is 0.
- </DL>
- <DL><DT><B>capitalize</B> (<VAR>word</VAR>) -- function of module string<DD>
- Capitalize the first character of the argument.
- </DL>
- <DL><DT><B>capwords</B> (<VAR>s</VAR>) -- function of module string<DD>
- Split the argument into words using <CODE>split</CODE>, capitalize each word
- using <CODE>capitalize</CODE>, and join the capitalized words using
- <CODE>join</CODE>. Note that this replaces runs of whitespace characters by
- a single space. (See also <CODE>regsub.capwords()</CODE> for a version
- that doesn't change the delimiters, and lets you specify a word
- separator.)
- </DL>
- <DL><DT><B>expandtabs</B> (<VAR>s</VAR>, <VAR>tabsize</VAR>) -- function of module string<DD>
- Expand tabs in a string, i.e. replace them by one or more spaces,
- depending on the current column and the given tab size. The column
- number is reset to zero after each newline occurring in the string.
- This doesn't understand other non-printing characters or escape
- sequences.
- </DL>
- <DL><DT><B>find</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
- Return the lowest index in <VAR>s</VAR> not smaller than <VAR>start</VAR> where the
- substring <VAR>sub</VAR> is found. Return <CODE>-1</CODE> when <VAR>sub</VAR>
- does not occur as a substring of <VAR>s</VAR> with index at least <VAR>start</VAR>.
- If <VAR>start</VAR> is omitted, it defaults to <CODE>0</CODE>. If <VAR>start</VAR> is
- negative, <CODE>len(<VAR>s</VAR>)</CODE> is added.
- </DL>
- <DL><DT><B>rfind</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
- Like <CODE>find</CODE> but find the highest index.
- </DL>
- <DL><DT><B>index</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
- Like <CODE>find</CODE> but raise <CODE>ValueError</CODE> when the substring is
- not found.
- </DL>
- <DL><DT><B>rindex</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
- Like <CODE>rfind</CODE> but raise <CODE>ValueError</CODE> when the substring is
- not found.
- </DL>
- <DL><DT><B>count</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
- Return the number of (non-overlapping) occurrences of substring
- <VAR>sub</VAR> in string <VAR>s</VAR> with index at least <VAR>start</VAR>.
- If <VAR>start</VAR> is omitted, it defaults to <CODE>0</CODE>. If <VAR>start</VAR> is
- negative, <CODE>len(<VAR>s</VAR>)</CODE> is added.
- </DL>
- <DL><DT><B>lower</B> (<VAR>s</VAR>) -- function of module string<DD>
- Convert letters to lower case.
- </DL>
- <DL><DT><B>maketrans</B> (<VAR>from</VAR>, <VAR>to</VAR>) -- function of module string<DD>
- Return a translation table suitable for passing to <CODE>string.translate</CODE>
- or <CODE>regex.compile</CODE>, that will map each character in <VAR>from</VAR>
- into the character at the same position in <VAR>to</VAR>; <VAR>from</VAR> and
- <VAR>to</VAR> must have the same length.
- </DL>
- <DL><DT><B>split</B> (<VAR>s</VAR>[, <VAR>sep</VAR>[, <VAR>maxsplit</VAR>]]) -- function of module string<DD>
- Return a list of the words of the string <VAR>s</VAR>. If the optional
- second argument <VAR>sep</VAR> is absent or <CODE>None</CODE>, the words are
- separated by arbitrary strings of whitespace characters (space, tab,
- newline, return, formfeed). If the second argument <VAR>sep</VAR> is
- present and not <CODE>None</CODE>, it specifies a string to be used as the
- word separator. The returned list will then have one more items than
- the number of non-overlapping occurrences of the separator in the
- string. The optional third argument <VAR>maxsplit</VAR> defaults to 0. If
- it is nonzero, at most <VAR>maxsplit</VAR> number of splits occur, and the
- remainder of the string is returned as the final element of the list
- (thus, the list will have at most <CODE><VAR>maxsplit</VAR>+1</CODE> elements).
- (See also <CODE>regsub.split()</CODE> for a version that allows specifying a
- regular expression as the separator.)
- </DL>
- <DL><DT><B>splitfields</B> (<VAR>s</VAR>[, <VAR>sep</VAR>[, <VAR>maxsplit</VAR>]]) -- function of module string<DD>
- This function behaves identical to <CODE>split</CODE>. (In the past,
- <CODE>split</CODE> was only used with one argument, while <CODE>splitfields</CODE>
- was only used with two arguments.)
- </DL>
- <DL><DT><B>join</B> (<VAR>words</VAR>[, <VAR>sep</VAR>]) -- function of module string<DD>
- Concatenate a list or tuple of words with intervening occurrences of
- <VAR>sep</VAR>. The default value for <VAR>sep</VAR> is a single space character.
- It is always true that
- <CODE>string.join(string.split(<VAR>s</VAR>, <VAR>sep</VAR>), <VAR>sep</VAR>)</CODE>
- equals <VAR>s</VAR>.
- </DL>
- <DL><DT><B>joinfields</B> (<VAR>words</VAR>[, <VAR>sep</VAR>]) -- function of module string<DD>
- This function behaves identical to <CODE>join</CODE>. (In the past,
- <CODE>join</CODE> was only used with one argument, while <CODE>joinfields</CODE>
- was only used with two arguments.)
- </DL>
- <DL><DT><B>lstrip</B> (<VAR>s</VAR>) -- function of module string<DD>
- Remove leading whitespace from the string <VAR>s</VAR>.
- </DL>
- <DL><DT><B>rstrip</B> (<VAR>s</VAR>) -- function of module string<DD>
- Remove trailing whitespace from the string <VAR>s</VAR>.
- </DL>
- <DL><DT><B>strip</B> (<VAR>s</VAR>) -- function of module string<DD>
- Remove leading and trailing whitespace from the string <VAR>s</VAR>.
- </DL>
- <DL><DT><B>swapcase</B> (<VAR>s</VAR>) -- function of module string<DD>
- Convert lower case letters to upper case and vice versa.
- </DL>
- <DL><DT><B>translate</B> (<VAR>s</VAR>, <VAR>table</VAR>[, <VAR>deletechars</VAR>]) -- function of module string<DD>
- Delete all characters from <VAR>s</VAR> that are in <VAR>deletechars</VAR> (if present), and
- then translate the characters using <VAR>table</VAR>, which must be
- a 256-character string giving the translation for each character
- value, indexed by its ordinal.
- </DL>
- <DL><DT><B>upper</B> (<VAR>s</VAR>) -- function of module string<DD>
- Convert letters to upper case.
- </DL>
- <DL><DT><B>ljust</B> (<VAR>s</VAR>, <VAR>width</VAR>) -- function of module string<DD>
- </DL>
- <DL><DT><B>rjust</B> (<VAR>s</VAR>, <VAR>width</VAR>) -- function of module string<DD>
- </DL>
- <DL><DT><B>center</B> (<VAR>s</VAR>, <VAR>width</VAR>) -- function of module string<DD>
- These functions respectively left-justify, right-justify and center a
- string in a field of given width.
- They return a string that is at least
- <VAR>width</VAR>
- characters wide, created by padding the string
- <VAR>s</VAR>
- with spaces until the given width on the right, left or both sides.
- The string is never truncated.
- </DL>
- <DL><DT><B>zfill</B> (<VAR>s</VAR>, <VAR>width</VAR>) -- function of module string<DD>
- Pad a numeric string on the left with zero digits until the given
- width is reached. Strings starting with a sign are handled correctly.
- </DL>
- This module is implemented in Python. Much of its functionality has
- been reimplemented in the built-in module <CODE>strop</CODE>. However, you
- should <I>never</I> import the latter module directly. When
- <CODE>string</CODE> discovers that <CODE>strop</CODE> exists, it transparently
- replaces parts of itself with the implementation from <CODE>strop</CODE>.
- After initialization, there is <I>no</I> overhead in using
- <CODE>string</CODE> instead of <CODE>strop</CODE>.
-