1998-08-10 16:42:37 -03:00
|
|
|
\section{\module{string} ---
|
1999-04-22 18:23:22 -03:00
|
|
|
Common string operations}
|
1998-07-23 14:59:49 -03:00
|
|
|
|
1999-04-22 18:23:22 -03:00
|
|
|
\declaremodule{standard}{string}
|
1998-07-23 14:59:49 -03:00
|
|
|
\modulesynopsis{Common string operations.}
|
|
|
|
|
1994-01-01 21:22:07 -04:00
|
|
|
|
|
|
|
This module defines some constants useful for checking character
|
1997-12-16 00:04:25 -04:00
|
|
|
classes and some useful string functions. See the module
|
1999-04-22 18:23:22 -03:00
|
|
|
\refmodule{re}\refstmodindex{re} for string functions based on regular
|
1998-03-17 02:33:25 -04:00
|
|
|
expressions.
|
1995-03-02 08:37:30 -04:00
|
|
|
|
|
|
|
The constants defined in this module are are:
|
1994-01-01 21:22:07 -04:00
|
|
|
|
|
|
|
\begin{datadesc}{digits}
|
|
|
|
The string \code{'0123456789'}.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{hexdigits}
|
|
|
|
The string \code{'0123456789abcdefABCDEF'}.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{letters}
|
2000-04-10 15:35:49 -03:00
|
|
|
The concatenation of the strings \constant{lowercase} and
|
|
|
|
\constant{uppercase} described below.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{lowercase}
|
|
|
|
A string containing all the characters that are considered lowercase
|
|
|
|
letters. On most systems this is the string
|
1995-02-28 13:14:32 -04:00
|
|
|
\code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition ---
|
1998-03-17 02:33:25 -04:00
|
|
|
the effect on the routines \function{upper()} and
|
|
|
|
\function{swapcase()} is undefined.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{octdigits}
|
|
|
|
The string \code{'01234567'}.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{uppercase}
|
|
|
|
A string containing all the characters that are considered uppercase
|
|
|
|
letters. On most systems this is the string
|
1995-02-28 13:14:32 -04:00
|
|
|
\code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition ---
|
1998-03-17 02:33:25 -04:00
|
|
|
the effect on the routines \function{lower()} and
|
|
|
|
\function{swapcase()} is undefined.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{whitespace}
|
|
|
|
A string containing all characters that are considered whitespace.
|
|
|
|
On most systems this includes the characters space, tab, linefeed,
|
1995-02-28 13:14:32 -04:00
|
|
|
return, formfeed, and vertical tab. Do not change its definition ---
|
1998-03-17 02:33:25 -04:00
|
|
|
the effect on the routines \function{strip()} and \function{split()}
|
|
|
|
is undefined.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{datadesc}
|
|
|
|
|
1995-03-02 08:37:30 -04:00
|
|
|
The functions defined in this module are:
|
1994-01-01 21:22:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
\begin{funcdesc}{atof}{s}
|
1998-12-21 14:56:13 -04:00
|
|
|
Convert a string to a floating point number. The string must have
|
|
|
|
the standard syntax for a floating point literal in Python,
|
1999-02-18 12:08:36 -04:00
|
|
|
optionally preceded by a sign (\samp{+} or \samp{-}). Note that
|
|
|
|
this behaves identical to the built-in function
|
|
|
|
\function{float()}\bifuncindex{float} when passed a string.
|
|
|
|
|
|
|
|
\strong{Note:} When passing in a string, values for NaN\index{NaN}
|
|
|
|
and Infinity\index{Infinity} may be returned, depending on the
|
|
|
|
underlying C library. The specific set of strings accepted which
|
|
|
|
cause these values to be returned depends entirely on the C library
|
|
|
|
and is known to vary.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{atoi}{s\optional{, base}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Convert string \var{s} to an integer in the given \var{base}. The
|
|
|
|
string must consist of one or more digits, optionally preceded by a
|
|
|
|
sign (\samp{+} or \samp{-}). The \var{base} 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} or \samp{0X}
|
|
|
|
means 16, \samp{0} means 8, anything else means 10. If \var{base}
|
|
|
|
is 16, a leading \samp{0x} or \samp{0X} is always accepted. Note
|
|
|
|
that when invoked without \var{base} or with \var{base} set to 10,
|
|
|
|
this behaves identical to the built-in function \function{int()}
|
|
|
|
when passed a string. (Also note: for a more flexible
|
|
|
|
interpretation of numeric literals, use the built-in function
|
|
|
|
\function{eval()}\bifuncindex{eval}.)
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{atol}{s\optional{, base}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Convert string \var{s} to a long integer in the given \var{base}.
|
|
|
|
The string must consist of one or more digits, optionally preceded
|
|
|
|
by a sign (\samp{+} or \samp{-}). The \var{base} argument has the
|
|
|
|
same meaning as for \function{atoi()}. A trailing \samp{l} or
|
|
|
|
\samp{L} is not allowed, except if the base is 0. Note that when
|
|
|
|
invoked without \var{base} or with \var{base} set to 10, this
|
|
|
|
behaves identical to the built-in function
|
|
|
|
\function{long()}\bifuncindex{long} when passed a string.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1996-08-09 18:44:51 -03:00
|
|
|
\begin{funcdesc}{capitalize}{word}
|
1998-12-21 14:56:13 -04:00
|
|
|
Capitalize the first character of the argument.
|
1996-08-09 18:44:51 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{capwords}{s}
|
1998-12-21 14:56:13 -04:00
|
|
|
Split the argument into words using \function{split()}, capitalize
|
|
|
|
each word using \function{capitalize()}, and join the capitalized
|
|
|
|
words using \function{join()}. Note that this replaces runs of
|
|
|
|
whitespace characters by a single space, and removes leading and
|
|
|
|
trailing whitespace.
|
1996-08-09 18:44:51 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1999-01-25 18:31:53 -04:00
|
|
|
\begin{funcdesc}{expandtabs}{s, \optional{tabsize}}
|
1998-12-21 14:56:13 -04:00
|
|
|
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
|
1999-01-25 18:31:53 -04:00
|
|
|
sequences. The tab size defaults to 8.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{find}{s, sub\optional{, start\optional{,end}}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return the lowest index in \var{s} where the substring \var{sub} is
|
|
|
|
found such that \var{sub} is wholly contained in
|
|
|
|
\code{\var{s}[\var{start}:\var{end}]}. Return \code{-1} on failure.
|
|
|
|
Defaults for \var{start} and \var{end} and interpretation of
|
|
|
|
negative values is the same as for slices.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{rfind}{s, sub\optional{, start\optional{, end}}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Like \function{find()} but find the highest index.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{index}{s, sub\optional{, start\optional{, end}}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Like \function{find()} but raise \exception{ValueError} when the
|
|
|
|
substring is not found.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{rindex}{s, sub\optional{, start\optional{, end}}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Like \function{rfind()} but raise \exception{ValueError} when the
|
|
|
|
substring is not found.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{count}{s, sub\optional{, start\optional{, end}}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return the number of (non-overlapping) occurrences of substring
|
|
|
|
\var{sub} in string \code{\var{s}[\var{start}:\var{end}]}.
|
|
|
|
Defaults for \var{start} and \var{end} and interpretation of
|
|
|
|
negative values is the same as for slices.
|
1994-08-01 09:18:36 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1994-01-01 21:22:07 -04:00
|
|
|
\begin{funcdesc}{lower}{s}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a copy of \var{s}, but with upper case letters converted to
|
|
|
|
lower case.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1996-07-30 15:23:05 -03:00
|
|
|
\begin{funcdesc}{maketrans}{from, to}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a translation table suitable for passing to
|
|
|
|
\function{translate()} or \function{regex.compile()}, that will map
|
|
|
|
each character in \var{from} into the character at the same position
|
|
|
|
in \var{to}; \var{from} and \var{to} must have the same length.
|
1998-06-11 13:03:30 -03:00
|
|
|
|
2000-04-10 15:35:49 -03:00
|
|
|
\strong{Warning:} don't use strings derived from \constant{lowercase}
|
|
|
|
and \constant{uppercase} as arguments; in some locales, these don't have
|
1998-12-21 14:56:13 -04:00
|
|
|
the same length. For case conversions, always use
|
|
|
|
\function{lower()} and \function{upper()}.
|
1996-07-30 15:23:05 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{split}{s\optional{, sep\optional{, maxsplit}}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a list of the words of the string \var{s}. If the optional
|
|
|
|
second argument \var{sep} is absent or \code{None}, the words are
|
|
|
|
separated by arbitrary strings of whitespace characters (space, tab,
|
|
|
|
newline, return, formfeed). If the second argument \var{sep} is
|
|
|
|
present and not \code{None}, it specifies a string to be used as the
|
1999-05-27 14:18:08 -03:00
|
|
|
word separator. The returned list will then have one more item
|
1998-12-21 14:56:13 -04:00
|
|
|
than the number of non-overlapping occurrences of the separator in
|
|
|
|
the string. The optional third argument \var{maxsplit} defaults to
|
|
|
|
0. If it is nonzero, at most \var{maxsplit} 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}+1}
|
|
|
|
elements).
|
1996-08-09 18:44:51 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{splitfields}{s\optional{, sep\optional{, maxsplit}}}
|
1998-12-21 14:56:13 -04:00
|
|
|
This function behaves identically to \function{split()}. (In the
|
|
|
|
past, \function{split()} was only used with one argument, while
|
|
|
|
\function{splitfields()} was only used with two arguments.)
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{join}{words\optional{, sep}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Concatenate a list or tuple of words with intervening occurrences of
|
|
|
|
\var{sep}. The default value for \var{sep} is a single space
|
|
|
|
character. It is always true that
|
|
|
|
\samp{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
|
|
|
|
equals \var{s}.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{joinfields}{words\optional{, sep}}
|
1998-12-21 14:56:13 -04:00
|
|
|
This function behaves identical to \function{join()}. (In the past,
|
|
|
|
\function{join()} was only used with one argument, while
|
|
|
|
\function{joinfields()} was only used with two arguments.)
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1996-08-09 18:44:51 -03:00
|
|
|
\begin{funcdesc}{lstrip}{s}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a copy of \var{s} but without leading whitespace characters.
|
1996-08-09 18:44:51 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{rstrip}{s}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a copy of \var{s} but without trailing whitespace
|
|
|
|
characters.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{strip}{s}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a copy of \var{s} without leading or trailing whitespace.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{swapcase}{s}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a copy of \var{s}, but with lower case letters
|
|
|
|
converted to upper case and vice versa.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1996-07-30 15:23:05 -03:00
|
|
|
\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Delete all characters from \var{s} that are in \var{deletechars} (if
|
|
|
|
present), and then translate the characters using \var{table}, which
|
|
|
|
must be a 256-character string giving the translation for each
|
|
|
|
character value, indexed by its ordinal.
|
1995-09-13 14:37:21 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1994-01-01 21:22:07 -04:00
|
|
|
\begin{funcdesc}{upper}{s}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a copy of \var{s}, but with lower case letters converted to
|
|
|
|
upper case.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{ljust}{s, width}
|
|
|
|
\funcline{rjust}{s, width}
|
|
|
|
\funcline{center}{s, width}
|
1998-12-21 14:56:13 -04:00
|
|
|
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} characters wide, created by padding the string
|
|
|
|
\var{s} with spaces until the given width on the right, left or both
|
|
|
|
sides. The string is never truncated.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{zfill}{s, width}
|
1998-12-21 14:56:13 -04:00
|
|
|
Pad a numeric string on the left with zero digits until the given
|
|
|
|
width is reached. Strings starting with a sign are handled
|
|
|
|
correctly.
|
1994-01-01 21:22:07 -04:00
|
|
|
\end{funcdesc}
|
1995-03-02 08:37:30 -04:00
|
|
|
|
1997-04-02 01:56:16 -04:00
|
|
|
\begin{funcdesc}{replace}{str, old, new\optional{, maxsplit}}
|
1998-12-21 14:56:13 -04:00
|
|
|
Return a copy of string \var{str} with all occurrences of substring
|
|
|
|
\var{old} replaced by \var{new}. If the optional argument
|
|
|
|
\var{maxsplit} is given, the first \var{maxsplit} occurrences are
|
|
|
|
replaced.
|
1997-03-25 12:41:31 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1995-03-02 08:37:30 -04:00
|
|
|
This module is implemented in Python. Much of its functionality has
|
1998-03-17 02:33:25 -04:00
|
|
|
been reimplemented in the built-in module
|
|
|
|
\module{strop}\refbimodindex{strop}. However, you
|
1995-03-02 08:37:30 -04:00
|
|
|
should \emph{never} import the latter module directly. When
|
1998-03-17 02:33:25 -04:00
|
|
|
\module{string} discovers that \module{strop} exists, it transparently
|
|
|
|
replaces parts of itself with the implementation from \module{strop}.
|
1995-03-02 08:37:30 -04:00
|
|
|
After initialization, there is \emph{no} overhead in using
|
1998-03-17 02:33:25 -04:00
|
|
|
\module{string} instead of \module{strop}.
|