blob: 7a5674e9359deb7325353407120e9705dadf62fa [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{string}}
2
3\stmodindex{string}
4
5This module defines some constants useful for checking character
Guido van Rossum0bf4d891995-03-02 12:37:30 +00006classes and some useful string functions. See the modules
7\code{regex} and \code{regsub} for string functions based on regular
8expressions.
9
10The constants defined in this module are are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011
12\renewcommand{\indexsubitem}{(data in module string)}
13\begin{datadesc}{digits}
14 The string \code{'0123456789'}.
15\end{datadesc}
16
17\begin{datadesc}{hexdigits}
18 The string \code{'0123456789abcdefABCDEF'}.
19\end{datadesc}
20
21\begin{datadesc}{letters}
22 The concatenation of the strings \code{lowercase} and
23 \code{uppercase} described below.
24\end{datadesc}
25
26\begin{datadesc}{lowercase}
27 A string containing all the characters that are considered lowercase
28 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000029 \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030 the effect on the routines \code{upper} and \code{swapcase} is
31 undefined.
32\end{datadesc}
33
34\begin{datadesc}{octdigits}
35 The string \code{'01234567'}.
36\end{datadesc}
37
38\begin{datadesc}{uppercase}
39 A string containing all the characters that are considered uppercase
40 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000041 \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042 the effect on the routines \code{lower} and \code{swapcase} is
43 undefined.
44\end{datadesc}
45
46\begin{datadesc}{whitespace}
47 A string containing all characters that are considered whitespace.
48 On most systems this includes the characters space, tab, linefeed,
Guido van Rossum86751151995-02-28 17:14:32 +000049 return, formfeed, and vertical tab. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000050 the effect on the routines \code{strip} and \code{split} is
51 undefined.
52\end{datadesc}
53
Guido van Rossum0bf4d891995-03-02 12:37:30 +000054The functions defined in this module are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055
56\renewcommand{\indexsubitem}{(in module string)}
57
58\begin{funcdesc}{atof}{s}
59Convert a string to a floating point number. The string must have
60the standard syntax for a floating point literal in Python, optionally
61preceded by a sign (\samp{+} or \samp{-}).
62\end{funcdesc}
63
Guido van Rossum470be141995-03-17 16:07:09 +000064\begin{funcdesc}{atoi}{s\optional{\, base}}
65Convert string \var{s} to an integer in the given \var{base}. The
66string must consist of one or more digits, optionally preceded by a
67sign (\samp{+} or \samp{-}). The \var{base} defaults to 10. If it is
680, a default base is chosen depending on the leading characters of the
69string (after stripping the sign): \samp{0x} or \samp{0X} means 16,
70\samp{0} means 8, anything else means 10. If \var{base} is 16, a
71leading \samp{0x} or \samp{0X} is always accepted. (Note: for a more
72flexible interpretation of numeric literals, use the built-in function
Guido van Rossum96628a91995-04-10 11:34:00 +000073\code{eval()}.)
Guido van Rossum470be141995-03-17 16:07:09 +000074\bifuncindex{eval}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075\end{funcdesc}
76
Guido van Rossum470be141995-03-17 16:07:09 +000077\begin{funcdesc}{atol}{s\optional{\, base}}
78Convert string \var{s} to a long integer in the given \var{base}. The
79string must consist of one or more digits, optionally preceded by a
80sign (\samp{+} or \samp{-}). The \var{base} argument has the same
81meaning as for \code{atoi()}. A trailing \samp{l} or \samp{L} is not
Guido van Rossum264302d1996-02-12 23:20:12 +000082allowed, except if the base is 0.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000083\end{funcdesc}
84
85\begin{funcdesc}{expandtabs}{s\, tabsize}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000086Expand tabs in a string, i.e.\ replace them by one or more spaces,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000087depending on the current column and the given tab size. The column
88number is reset to zero after each newline occurring in the string.
89This doesn't understand other non-printing characters or escape
90sequences.
91\end{funcdesc}
92
Guido van Rossum16d6e711994-08-08 12:30:22 +000093\begin{funcdesc}{find}{s\, sub\optional{\, start}}
94Return the lowest index in \var{s} not smaller than \var{start} where the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095substring \var{sub} is found. Return \code{-1} when \var{sub}
Guido van Rossum16d6e711994-08-08 12:30:22 +000096does not occur as a substring of \var{s} with index at least \var{start}.
97If \var{start} is omitted, it defaults to \code{0}. If \var{start} is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098negative, \code{len(\var{s})} is added.
99\end{funcdesc}
100
Guido van Rossum16d6e711994-08-08 12:30:22 +0000101\begin{funcdesc}{rfind}{s\, sub\optional{\, start}}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000102Like \code{find} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000103\end{funcdesc}
104
Guido van Rossum16d6e711994-08-08 12:30:22 +0000105\begin{funcdesc}{index}{s\, sub\optional{\, start}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000106Like \code{find} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000107not found.
108\end{funcdesc}
109
Guido van Rossum16d6e711994-08-08 12:30:22 +0000110\begin{funcdesc}{rindex}{s\, sub\optional{\, start}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000111Like \code{rfind} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000112not found.
113\end{funcdesc}
114
Guido van Rossum470be141995-03-17 16:07:09 +0000115\begin{funcdesc}{count}{s\, sub\optional{\, start}}
Guido van Rossumab3a2501994-08-01 12:18:36 +0000116Return the number of (non-overlapping) occurrences of substring
Guido van Rossum470be141995-03-17 16:07:09 +0000117\var{sub} in string \var{s} with index at least \var{start}.
118If \var{start} is omitted, it defaults to \code{0}. If \var{start} is
119negative, \code{len(\var{s})} is added.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000120\end{funcdesc}
121
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000122\begin{funcdesc}{lower}{s}
123Convert letters to lower case.
124\end{funcdesc}
125
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000126\begin{funcdesc}{maketrans}{from, to}
127Return a translation table suitable for passing to \code{string.translate}
128or \code{regex.compile}, that will map each character in \var{from}
129into the character at the same position in \var{to}; \var{from} and
130\var{to} must have the same length.
131\end{funcdesc}
132
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000133\begin{funcdesc}{split}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000134Return a list of the whitespace-delimited words of the string
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000135\var{s}.
136\end{funcdesc}
137
138\begin{funcdesc}{splitfields}{s\, sep}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000139 Return a list containing the fields of the string \var{s}, using
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000140 the string \var{sep} as a separator. The list will have one more
141 items than the number of non-overlapping occurrences of the
142 separator in the string. Thus, \code{string.splitfields(\var{s}, '
143 ')} is not the same as \code{string.split(\var{s})}, as the latter
144 only returns non-empty words. As a special case,
145 \code{splitfields(\var{s}, '')} returns \code{[\var{s}]}, for any string
146 \var{s}. (See also \code{regsub.split()}.)
147\end{funcdesc}
148
149\begin{funcdesc}{join}{words}
150Concatenate a list or tuple of words with intervening spaces.
151\end{funcdesc}
152
153\begin{funcdesc}{joinfields}{words\, sep}
154Concatenate a list or tuple of words with intervening separators.
155It is always true that
156\code{string.joinfields(string.splitfields(\var{t}, \var{sep}), \var{sep})}
157equals \var{t}.
158\end{funcdesc}
159
160\begin{funcdesc}{strip}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000161Remove leading and trailing whitespace from the string
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000162\var{s}.
163\end{funcdesc}
164
165\begin{funcdesc}{swapcase}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000166Convert lower case letters to upper case and vice versa.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000167\end{funcdesc}
168
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000169\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
170Delete all characters from \var{s} that are in \var{deletechars} (if present), and
171then translate the characters using \var{table}, which must be
Guido van Rossumf65f2781995-09-13 17:37:21 +0000172a 256-character string giving the translation for each character
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000173value, indexed by its ordinal.
Guido van Rossumf65f2781995-09-13 17:37:21 +0000174\end{funcdesc}
175
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000176\begin{funcdesc}{upper}{s}
177Convert letters to upper case.
178\end{funcdesc}
179
180\begin{funcdesc}{ljust}{s\, width}
181\funcline{rjust}{s\, width}
182\funcline{center}{s\, width}
183These functions respectively left-justify, right-justify and center a
184string in a field of given width.
185They return a string that is at least
186\var{width}
187characters wide, created by padding the string
188\var{s}
189with spaces until the given width on the right, left or both sides.
190The string is never truncated.
191\end{funcdesc}
192
193\begin{funcdesc}{zfill}{s\, width}
194Pad a numeric string on the left with zero digits until the given
195width is reached. Strings starting with a sign are handled correctly.
196\end{funcdesc}
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000197
198This module is implemented in Python. Much of its functionality has
199been reimplemented in the built-in module \code{strop}. However, you
200should \emph{never} import the latter module directly. When
201\code{string} discovers that \code{strop} exists, it transparently
202replaces parts of itself with the implementation from \code{strop}.
203After initialization, there is \emph{no} overhead in using
204\code{string} instead of \code{strop}.
205\bimodindex{strop}