blob: 7242c5332359082faa49b6864756d6a9c81b20d8 [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
Fred Drake6d2bdb61997-12-16 04:04:25 +00007classes and some useful string functions. See the module
8\code{re} for string functions based on regular expressions.
9\refstmodindex{re}
Guido van Rossum0bf4d891995-03-02 12:37:30 +000010
11The constants defined in this module are are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012
Fred Drake19479911998-02-13 06:58:54 +000013\setindexsubitem{(data in module string)}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014\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
Fred Drake19479911998-02-13 06:58:54 +000057\setindexsubitem{(in module string)}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058
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
Guido van Rossumad37e9e1998-02-02 03:01:10 +000099a single space, and removes leading and trailing whitespace.
Guido van Rossume5e55d71996-08-09 21:44:51 +0000100\end{funcdesc}
101
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000102\begin{funcdesc}{expandtabs}{s\, tabsize}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000103Expand tabs in a string, i.e.\ replace them by one or more spaces,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104depending on the current column and the given tab size. The column
105number is reset to zero after each newline occurring in the string.
106This doesn't understand other non-printing characters or escape
107sequences.
108\end{funcdesc}
109
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000110\begin{funcdesc}{find}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000111Return the lowest index in \var{s} where the substring \var{sub} is
112found such that \var{sub} is wholly contained in
113\code{\var{s}[\var{start}:\var{end}]}. Return -1 on failure.
114Defaults for \var{start} and \var{end} and interpretation of negative
115values is the same as for slices.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000116\end{funcdesc}
117
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000118\begin{funcdesc}{rfind}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000119Like \code{find} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120\end{funcdesc}
121
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000122\begin{funcdesc}{index}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000123Like \code{find} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124not found.
125\end{funcdesc}
126
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000127\begin{funcdesc}{rindex}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000128Like \code{rfind} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000129not found.
130\end{funcdesc}
131
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000132\begin{funcdesc}{count}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossumab3a2501994-08-01 12:18:36 +0000133Return the number of (non-overlapping) occurrences of substring
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000134\var{sub} in string \code{\var{s}[\var{start}:\var{end}]}.
135Defaults for \var{start} and \var{end} and interpretation of negative
136values is the same as for slices.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000137\end{funcdesc}
138
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000139\begin{funcdesc}{lower}{s}
140Convert letters to lower case.
141\end{funcdesc}
142
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000143\begin{funcdesc}{maketrans}{from, to}
144Return a translation table suitable for passing to \code{string.translate}
145or \code{regex.compile}, that will map each character in \var{from}
146into the character at the same position in \var{to}; \var{from} and
147\var{to} must have the same length.
148\end{funcdesc}
149
Guido van Rossume5e55d71996-08-09 21:44:51 +0000150\begin{funcdesc}{split}{s\optional{\, sep\optional{\, maxsplit}}}
151Return a list of the words of the string \var{s}. If the optional
152second argument \var{sep} is absent or \code{None}, the words are
153separated by arbitrary strings of whitespace characters (space, tab,
154newline, return, formfeed). If the second argument \var{sep} is
155present and not \code{None}, it specifies a string to be used as the
156word separator. The returned list will then have one more items than
157the number of non-overlapping occurrences of the separator in the
158string. The optional third argument \var{maxsplit} defaults to 0. If
159it is nonzero, at most \var{maxsplit} number of splits occur, and the
160remainder of the string is returned as the final element of the list
161(thus, the list will have at most \code{\var{maxsplit}+1} elements).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000162\end{funcdesc}
163
Guido van Rossume5e55d71996-08-09 21:44:51 +0000164\begin{funcdesc}{splitfields}{s\optional{\, sep\optional{\, maxsplit}}}
Guido van Rossum0fa066b1997-06-02 17:30:20 +0000165This function behaves identically to \code{split}. (In the past,
Guido van Rossume5e55d71996-08-09 21:44:51 +0000166\code{split} was only used with one argument, while \code{splitfields}
167was only used with two arguments.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000168\end{funcdesc}
169
Guido van Rossume5e55d71996-08-09 21:44:51 +0000170\begin{funcdesc}{join}{words\optional{\, sep}}
171Concatenate a list or tuple of words with intervening occurrences of
172\var{sep}. The default value for \var{sep} is a single space character.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000173It is always true that
Guido van Rossume5e55d71996-08-09 21:44:51 +0000174\code{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
175equals \var{s}.
176\end{funcdesc}
177
178\begin{funcdesc}{joinfields}{words\optional{\, sep}}
179This function behaves identical to \code{join}. (In the past,
180\code{join} was only used with one argument, while \code{joinfields}
181was only used with two arguments.)
182\end{funcdesc}
183
184\begin{funcdesc}{lstrip}{s}
185Remove leading whitespace from the string \var{s}.
186\end{funcdesc}
187
188\begin{funcdesc}{rstrip}{s}
189Remove trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000190\end{funcdesc}
191
192\begin{funcdesc}{strip}{s}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000193Remove leading and trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194\end{funcdesc}
195
196\begin{funcdesc}{swapcase}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000197Convert lower case letters to upper case and vice versa.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000198\end{funcdesc}
199
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000200\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
201Delete all characters from \var{s} that are in \var{deletechars} (if present), and
202then translate the characters using \var{table}, which must be
Guido van Rossumf65f2781995-09-13 17:37:21 +0000203a 256-character string giving the translation for each character
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000204value, indexed by its ordinal.
Guido van Rossumf65f2781995-09-13 17:37:21 +0000205\end{funcdesc}
206
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000207\begin{funcdesc}{upper}{s}
208Convert letters to upper case.
209\end{funcdesc}
210
211\begin{funcdesc}{ljust}{s\, width}
212\funcline{rjust}{s\, width}
213\funcline{center}{s\, width}
214These functions respectively left-justify, right-justify and center a
215string in a field of given width.
216They return a string that is at least
217\var{width}
218characters wide, created by padding the string
219\var{s}
220with spaces until the given width on the right, left or both sides.
221The string is never truncated.
222\end{funcdesc}
223
224\begin{funcdesc}{zfill}{s\, width}
225Pad a numeric string on the left with zero digits until the given
226width is reached. Strings starting with a sign are handled correctly.
227\end{funcdesc}
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000228
Guido van Rossum740eb821997-04-02 05:56:16 +0000229\begin{funcdesc}{replace}{str, old, new\optional{, maxsplit}}
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000230Return a copy of string \var{str} with all occurrences of substring
Guido van Rossum740eb821997-04-02 05:56:16 +0000231\var{old} replaced by \var{new}. If the optional argument
232\var{maxsplit} is given, the first \var{maxsplit} occurrences are
233replaced.
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000234\end{funcdesc}
235
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000236This module is implemented in Python. Much of its functionality has
237been reimplemented in the built-in module \code{strop}. However, you
238should \emph{never} import the latter module directly. When
239\code{string} discovers that \code{strop} exists, it transparently
240replaces parts of itself with the implementation from \code{strop}.
241After initialization, there is \emph{no} overhead in using
242\code{string} instead of \code{strop}.
Fred Drake6d2bdb61997-12-16 04:04:25 +0000243\refbimodindex{strop}