blob: 3d0631a4351980393143f4bfd78bbaeec08f74e5 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Standard Module \sectcode{string}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-string}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003
4\stmodindex{string}
5
6This module defines some constants useful for checking character
Guido van Rossum0bf4d891995-03-02 12:37:30 +00007classes and some useful string functions. See the modules
8\code{regex} and \code{regsub} for string functions based on regular
9expressions.
10
11The constants defined in this module are are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012
13\renewcommand{\indexsubitem}{(data in module string)}
14\begin{datadesc}{digits}
15 The string \code{'0123456789'}.
16\end{datadesc}
17
18\begin{datadesc}{hexdigits}
19 The string \code{'0123456789abcdefABCDEF'}.
20\end{datadesc}
21
22\begin{datadesc}{letters}
23 The concatenation of the strings \code{lowercase} and
24 \code{uppercase} described below.
25\end{datadesc}
26
27\begin{datadesc}{lowercase}
28 A string containing all the characters that are considered lowercase
29 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000030 \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031 the effect on the routines \code{upper} and \code{swapcase} is
32 undefined.
33\end{datadesc}
34
35\begin{datadesc}{octdigits}
36 The string \code{'01234567'}.
37\end{datadesc}
38
39\begin{datadesc}{uppercase}
40 A string containing all the characters that are considered uppercase
41 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000042 \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000043 the effect on the routines \code{lower} and \code{swapcase} is
44 undefined.
45\end{datadesc}
46
47\begin{datadesc}{whitespace}
48 A string containing all characters that are considered whitespace.
49 On most systems this includes the characters space, tab, linefeed,
Guido van Rossum86751151995-02-28 17:14:32 +000050 return, formfeed, and vertical tab. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051 the effect on the routines \code{strip} and \code{split} is
52 undefined.
53\end{datadesc}
54
Guido van Rossum0bf4d891995-03-02 12:37:30 +000055The functions defined in this module are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056
57\renewcommand{\indexsubitem}{(in module string)}
58
59\begin{funcdesc}{atof}{s}
60Convert a string to a floating point number. The string must have
61the standard syntax for a floating point literal in Python, optionally
Guido van Rossum740eb821997-04-02 05:56:16 +000062preceded by a sign (\samp{+} or \samp{-}). Note that this behaves
63identical to the built-in function \code{float()} when passed a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064\end{funcdesc}
65
Guido van Rossum470be141995-03-17 16:07:09 +000066\begin{funcdesc}{atoi}{s\optional{\, base}}
67Convert string \var{s} to an integer in the given \var{base}. The
68string must consist of one or more digits, optionally preceded by a
69sign (\samp{+} or \samp{-}). The \var{base} defaults to 10. If it is
700, a default base is chosen depending on the leading characters of the
71string (after stripping the sign): \samp{0x} or \samp{0X} means 16,
72\samp{0} means 8, anything else means 10. If \var{base} is 16, a
Guido van Rossum740eb821997-04-02 05:56:16 +000073leading \samp{0x} or \samp{0X} is always accepted. Note that when
74invoked without \var{base} or with \var{base} set to 10, this behaves
75identical to the built-in function \code{int()} when passed a string.
76(Also note: for a more flexible interpretation of numeric literals,
77use the built-in function \code{eval()}.)
Guido van Rossum470be141995-03-17 16:07:09 +000078\bifuncindex{eval}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\end{funcdesc}
80
Guido van Rossum470be141995-03-17 16:07:09 +000081\begin{funcdesc}{atol}{s\optional{\, base}}
82Convert string \var{s} to a long integer in the given \var{base}. The
83string must consist of one or more digits, optionally preceded by a
84sign (\samp{+} or \samp{-}). The \var{base} argument has the same
85meaning as for \code{atoi()}. A trailing \samp{l} or \samp{L} is not
Guido van Rossum740eb821997-04-02 05:56:16 +000086allowed, except if the base is 0. Note that when invoked without
87\var{base} or with \var{base} set to 10, this behaves identical to the
88built-in function \code{long()} when passed a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089\end{funcdesc}
90
Guido van Rossume5e55d71996-08-09 21:44:51 +000091\begin{funcdesc}{capitalize}{word}
92Capitalize the first character of the argument.
93\end{funcdesc}
94
95\begin{funcdesc}{capwords}{s}
96Split the argument into words using \code{split}, capitalize each word
97using \code{capitalize}, and join the capitalized words using
98\code{join}. Note that this replaces runs of whitespace characters by
99a single space. (See also \code{regsub.capwords()} for a version
100that doesn't change the delimiters, and lets you specify a word
101separator.)
102\end{funcdesc}
103
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104\begin{funcdesc}{expandtabs}{s\, tabsize}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000105Expand tabs in a string, i.e.\ replace them by one or more spaces,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000106depending on the current column and the given tab size. The column
107number is reset to zero after each newline occurring in the string.
108This doesn't understand other non-printing characters or escape
109sequences.
110\end{funcdesc}
111
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000112\begin{funcdesc}{find}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000113Return the lowest index in \var{s} where the substring \var{sub} is
114found such that \var{sub} is wholly contained in
115\code{\var{s}[\var{start}:\var{end}]}. Return -1 on failure.
116Defaults for \var{start} and \var{end} and interpretation of negative
117values is the same as for slices.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000118\end{funcdesc}
119
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000120\begin{funcdesc}{rfind}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000121Like \code{find} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000122\end{funcdesc}
123
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000124\begin{funcdesc}{index}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000125Like \code{find} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000126not found.
127\end{funcdesc}
128
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000129\begin{funcdesc}{rindex}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000130Like \code{rfind} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000131not found.
132\end{funcdesc}
133
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000134\begin{funcdesc}{count}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossumab3a2501994-08-01 12:18:36 +0000135Return the number of (non-overlapping) occurrences of substring
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000136\var{sub} in string \code{\var{s}[\var{start}:\var{end}]}.
137Defaults for \var{start} and \var{end} and interpretation of negative
138values is the same as for slices.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000139\end{funcdesc}
140
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000141\begin{funcdesc}{lower}{s}
142Convert letters to lower case.
143\end{funcdesc}
144
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000145\begin{funcdesc}{maketrans}{from, to}
146Return a translation table suitable for passing to \code{string.translate}
147or \code{regex.compile}, that will map each character in \var{from}
148into the character at the same position in \var{to}; \var{from} and
149\var{to} must have the same length.
150\end{funcdesc}
151
Guido van Rossume5e55d71996-08-09 21:44:51 +0000152\begin{funcdesc}{split}{s\optional{\, sep\optional{\, maxsplit}}}
153Return a list of the words of the string \var{s}. If the optional
154second argument \var{sep} is absent or \code{None}, the words are
155separated by arbitrary strings of whitespace characters (space, tab,
156newline, return, formfeed). If the second argument \var{sep} is
157present and not \code{None}, it specifies a string to be used as the
158word separator. The returned list will then have one more items than
159the number of non-overlapping occurrences of the separator in the
160string. The optional third argument \var{maxsplit} defaults to 0. If
161it is nonzero, at most \var{maxsplit} number of splits occur, and the
162remainder of the string is returned as the final element of the list
163(thus, the list will have at most \code{\var{maxsplit}+1} elements).
164(See also \code{regsub.split()} for a version that allows specifying a
165regular expression as the separator.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000166\end{funcdesc}
167
Guido van Rossume5e55d71996-08-09 21:44:51 +0000168\begin{funcdesc}{splitfields}{s\optional{\, sep\optional{\, maxsplit}}}
Guido van Rossum0fa066b1997-06-02 17:30:20 +0000169This function behaves identically to \code{split}. (In the past,
Guido van Rossume5e55d71996-08-09 21:44:51 +0000170\code{split} was only used with one argument, while \code{splitfields}
171was only used with two arguments.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000172\end{funcdesc}
173
Guido van Rossume5e55d71996-08-09 21:44:51 +0000174\begin{funcdesc}{join}{words\optional{\, sep}}
175Concatenate a list or tuple of words with intervening occurrences of
176\var{sep}. The default value for \var{sep} is a single space character.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000177It is always true that
Guido van Rossume5e55d71996-08-09 21:44:51 +0000178\code{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
179equals \var{s}.
180\end{funcdesc}
181
182\begin{funcdesc}{joinfields}{words\optional{\, sep}}
183This function behaves identical to \code{join}. (In the past,
184\code{join} was only used with one argument, while \code{joinfields}
185was only used with two arguments.)
186\end{funcdesc}
187
188\begin{funcdesc}{lstrip}{s}
189Remove leading whitespace from the string \var{s}.
190\end{funcdesc}
191
192\begin{funcdesc}{rstrip}{s}
193Remove trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194\end{funcdesc}
195
196\begin{funcdesc}{strip}{s}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000197Remove leading and trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000198\end{funcdesc}
199
200\begin{funcdesc}{swapcase}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000201Convert lower case letters to upper case and vice versa.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000202\end{funcdesc}
203
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000204\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
205Delete all characters from \var{s} that are in \var{deletechars} (if present), and
206then translate the characters using \var{table}, which must be
Guido van Rossumf65f2781995-09-13 17:37:21 +0000207a 256-character string giving the translation for each character
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000208value, indexed by its ordinal.
Guido van Rossumf65f2781995-09-13 17:37:21 +0000209\end{funcdesc}
210
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000211\begin{funcdesc}{upper}{s}
212Convert letters to upper case.
213\end{funcdesc}
214
215\begin{funcdesc}{ljust}{s\, width}
216\funcline{rjust}{s\, width}
217\funcline{center}{s\, width}
218These functions respectively left-justify, right-justify and center a
219string in a field of given width.
220They return a string that is at least
221\var{width}
222characters wide, created by padding the string
223\var{s}
224with spaces until the given width on the right, left or both sides.
225The string is never truncated.
226\end{funcdesc}
227
228\begin{funcdesc}{zfill}{s\, width}
229Pad a numeric string on the left with zero digits until the given
230width is reached. Strings starting with a sign are handled correctly.
231\end{funcdesc}
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000232
Guido van Rossum740eb821997-04-02 05:56:16 +0000233\begin{funcdesc}{replace}{str, old, new\optional{, maxsplit}}
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000234Return a copy of string \var{str} with all occurrences of substring
Guido van Rossum740eb821997-04-02 05:56:16 +0000235\var{old} replaced by \var{new}. If the optional argument
236\var{maxsplit} is given, the first \var{maxsplit} occurrences are
237replaced.
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000238\end{funcdesc}
239
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000240This module is implemented in Python. Much of its functionality has
241been reimplemented in the built-in module \code{strop}. However, you
242should \emph{never} import the latter module directly. When
243\code{string} discovers that \code{strop} exists, it transparently
244replaces parts of itself with the implementation from \code{strop}.
245After initialization, there is \emph{no} overhead in using
246\code{string} instead of \code{strop}.
247\bimodindex{strop}