blob: 7b91717e2219cb71667af93337e8ecf8ade34b01 [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
Guido van Rossum740eb821997-04-02 05:56:16 +000061preceded by a sign (\samp{+} or \samp{-}). Note that this behaves
62identical to the built-in function \code{float()} when passed a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063\end{funcdesc}
64
Guido van Rossum470be141995-03-17 16:07:09 +000065\begin{funcdesc}{atoi}{s\optional{\, base}}
66Convert string \var{s} to an integer in the given \var{base}. The
67string must consist of one or more digits, optionally preceded by a
68sign (\samp{+} or \samp{-}). The \var{base} defaults to 10. If it is
690, a default base is chosen depending on the leading characters of the
70string (after stripping the sign): \samp{0x} or \samp{0X} means 16,
71\samp{0} means 8, anything else means 10. If \var{base} is 16, a
Guido van Rossum740eb821997-04-02 05:56:16 +000072leading \samp{0x} or \samp{0X} is always accepted. Note that when
73invoked without \var{base} or with \var{base} set to 10, this behaves
74identical to the built-in function \code{int()} when passed a string.
75(Also note: for a more flexible interpretation of numeric literals,
76use the built-in function \code{eval()}.)
Guido van Rossum470be141995-03-17 16:07:09 +000077\bifuncindex{eval}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078\end{funcdesc}
79
Guido van Rossum470be141995-03-17 16:07:09 +000080\begin{funcdesc}{atol}{s\optional{\, base}}
81Convert string \var{s} to a long integer in the given \var{base}. The
82string must consist of one or more digits, optionally preceded by a
83sign (\samp{+} or \samp{-}). The \var{base} argument has the same
84meaning as for \code{atoi()}. A trailing \samp{l} or \samp{L} is not
Guido van Rossum740eb821997-04-02 05:56:16 +000085allowed, except if the base is 0. Note that when invoked without
86\var{base} or with \var{base} set to 10, this behaves identical to the
87built-in function \code{long()} when passed a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088\end{funcdesc}
89
Guido van Rossume5e55d71996-08-09 21:44:51 +000090\begin{funcdesc}{capitalize}{word}
91Capitalize the first character of the argument.
92\end{funcdesc}
93
94\begin{funcdesc}{capwords}{s}
95Split the argument into words using \code{split}, capitalize each word
96using \code{capitalize}, and join the capitalized words using
97\code{join}. Note that this replaces runs of whitespace characters by
98a single space. (See also \code{regsub.capwords()} for a version
99that doesn't change the delimiters, and lets you specify a word
100separator.)
101\end{funcdesc}
102
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000103\begin{funcdesc}{expandtabs}{s\, tabsize}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000104Expand tabs in a string, i.e.\ replace them by one or more spaces,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105depending on the current column and the given tab size. The column
106number is reset to zero after each newline occurring in the string.
107This doesn't understand other non-printing characters or escape
108sequences.
109\end{funcdesc}
110
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000111\begin{funcdesc}{find}{s\, sub\optional{\, start\optional{\,end}}}
112Return the lowest index in \var{s} not smaller than \var{start} and not
113greater than \var{end} where the substring \var{sub} is found. Return
114\code{-1} when \var{sub} does not occur as a substring of \var{s} with
115index at least \var{start} and less than \var{end}.
Guido van Rossum16d6e711994-08-08 12:30:22 +0000116If \var{start} is omitted, it defaults to \code{0}. If \var{start} is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000117negative, \code{len(\var{s})} is added.
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000118If \var{end} is omitted, it defaults to \code{len(\var{s})}. If
119\var{end} is negative, \code{len(\var{s})} is added.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120\end{funcdesc}
121
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000122\begin{funcdesc}{rfind}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000123Like \code{find} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124\end{funcdesc}
125
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000126\begin{funcdesc}{index}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000127Like \code{find} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000128not found.
129\end{funcdesc}
130
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000131\begin{funcdesc}{rindex}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000132Like \code{rfind} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000133not found.
134\end{funcdesc}
135
Guido van Rossum470be141995-03-17 16:07:09 +0000136\begin{funcdesc}{count}{s\, sub\optional{\, start}}
Guido van Rossumab3a2501994-08-01 12:18:36 +0000137Return the number of (non-overlapping) occurrences of substring
Guido van Rossum470be141995-03-17 16:07:09 +0000138\var{sub} in string \var{s} with index at least \var{start}.
139If \var{start} is omitted, it defaults to \code{0}. If \var{start} is
140negative, \code{len(\var{s})} is added.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000141\end{funcdesc}
142
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000143\begin{funcdesc}{lower}{s}
144Convert letters to lower case.
145\end{funcdesc}
146
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000147\begin{funcdesc}{maketrans}{from, to}
148Return a translation table suitable for passing to \code{string.translate}
149or \code{regex.compile}, that will map each character in \var{from}
150into the character at the same position in \var{to}; \var{from} and
151\var{to} must have the same length.
152\end{funcdesc}
153
Guido van Rossume5e55d71996-08-09 21:44:51 +0000154\begin{funcdesc}{split}{s\optional{\, sep\optional{\, maxsplit}}}
155Return a list of the words of the string \var{s}. If the optional
156second argument \var{sep} is absent or \code{None}, the words are
157separated by arbitrary strings of whitespace characters (space, tab,
158newline, return, formfeed). If the second argument \var{sep} is
159present and not \code{None}, it specifies a string to be used as the
160word separator. The returned list will then have one more items than
161the number of non-overlapping occurrences of the separator in the
162string. The optional third argument \var{maxsplit} defaults to 0. If
163it is nonzero, at most \var{maxsplit} number of splits occur, and the
164remainder of the string is returned as the final element of the list
165(thus, the list will have at most \code{\var{maxsplit}+1} elements).
166(See also \code{regsub.split()} for a version that allows specifying a
167regular expression as the separator.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000168\end{funcdesc}
169
Guido van Rossume5e55d71996-08-09 21:44:51 +0000170\begin{funcdesc}{splitfields}{s\optional{\, sep\optional{\, maxsplit}}}
Guido van Rossum0fa066b1997-06-02 17:30:20 +0000171This function behaves identically to \code{split}. (In the past,
Guido van Rossume5e55d71996-08-09 21:44:51 +0000172\code{split} was only used with one argument, while \code{splitfields}
173was only used with two arguments.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000174\end{funcdesc}
175
Guido van Rossume5e55d71996-08-09 21:44:51 +0000176\begin{funcdesc}{join}{words\optional{\, sep}}
177Concatenate a list or tuple of words with intervening occurrences of
178\var{sep}. The default value for \var{sep} is a single space character.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000179It is always true that
Guido van Rossume5e55d71996-08-09 21:44:51 +0000180\code{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
181equals \var{s}.
182\end{funcdesc}
183
184\begin{funcdesc}{joinfields}{words\optional{\, sep}}
185This function behaves identical to \code{join}. (In the past,
186\code{join} was only used with one argument, while \code{joinfields}
187was only used with two arguments.)
188\end{funcdesc}
189
190\begin{funcdesc}{lstrip}{s}
191Remove leading whitespace from the string \var{s}.
192\end{funcdesc}
193
194\begin{funcdesc}{rstrip}{s}
195Remove trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000196\end{funcdesc}
197
198\begin{funcdesc}{strip}{s}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000199Remove leading and trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000200\end{funcdesc}
201
202\begin{funcdesc}{swapcase}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000203Convert lower case letters to upper case and vice versa.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000204\end{funcdesc}
205
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000206\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
207Delete all characters from \var{s} that are in \var{deletechars} (if present), and
208then translate the characters using \var{table}, which must be
Guido van Rossumf65f2781995-09-13 17:37:21 +0000209a 256-character string giving the translation for each character
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000210value, indexed by its ordinal.
Guido van Rossumf65f2781995-09-13 17:37:21 +0000211\end{funcdesc}
212
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000213\begin{funcdesc}{upper}{s}
214Convert letters to upper case.
215\end{funcdesc}
216
217\begin{funcdesc}{ljust}{s\, width}
218\funcline{rjust}{s\, width}
219\funcline{center}{s\, width}
220These functions respectively left-justify, right-justify and center a
221string in a field of given width.
222They return a string that is at least
223\var{width}
224characters wide, created by padding the string
225\var{s}
226with spaces until the given width on the right, left or both sides.
227The string is never truncated.
228\end{funcdesc}
229
230\begin{funcdesc}{zfill}{s\, width}
231Pad a numeric string on the left with zero digits until the given
232width is reached. Strings starting with a sign are handled correctly.
233\end{funcdesc}
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000234
Guido van Rossum740eb821997-04-02 05:56:16 +0000235\begin{funcdesc}{replace}{str, old, new\optional{, maxsplit}}
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000236Return a copy of string \var{str} with all occurrences of substring
Guido van Rossum740eb821997-04-02 05:56:16 +0000237\var{old} replaced by \var{new}. If the optional argument
238\var{maxsplit} is given, the first \var{maxsplit} occurrences are
239replaced.
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000240\end{funcdesc}
241
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000242This module is implemented in Python. Much of its functionality has
243been reimplemented in the built-in module \code{strop}. However, you
244should \emph{never} import the latter module directly. When
245\code{string} discovers that \code{strop} exists, it transparently
246replaces parts of itself with the implementation from \code{strop}.
247After initialization, there is \emph{no} overhead in using
248\code{string} instead of \code{strop}.
249\bimodindex{strop}