blob: 930ce22d1dc18adf89b46d821d925574b85666fc [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}}}
113Return the lowest index in \var{s} not smaller than \var{start} and not
114greater than \var{end} where the substring \var{sub} is found. Return
115\code{-1} when \var{sub} does not occur as a substring of \var{s} with
116index at least \var{start} and less than \var{end}.
Guido van Rossum16d6e711994-08-08 12:30:22 +0000117If \var{start} is omitted, it defaults to \code{0}. If \var{start} is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000118negative, \code{len(\var{s})} is added.
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000119If \var{end} is omitted, it defaults to \code{len(\var{s})}. If
120\var{end} is negative, \code{len(\var{s})} is added.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000121\end{funcdesc}
122
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000123\begin{funcdesc}{rfind}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000124Like \code{find} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\end{funcdesc}
126
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000127\begin{funcdesc}{index}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000128Like \code{find} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000129not found.
130\end{funcdesc}
131
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000132\begin{funcdesc}{rindex}{s\, sub\optional{\, start\optional{\,end}}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000133Like \code{rfind} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134not found.
135\end{funcdesc}
136
Guido van Rossum470be141995-03-17 16:07:09 +0000137\begin{funcdesc}{count}{s\, sub\optional{\, start}}
Guido van Rossumab3a2501994-08-01 12:18:36 +0000138Return the number of (non-overlapping) occurrences of substring
Guido van Rossum470be141995-03-17 16:07:09 +0000139\var{sub} in string \var{s} with index at least \var{start}.
140If \var{start} is omitted, it defaults to \code{0}. If \var{start} is
141negative, \code{len(\var{s})} is added.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000142\end{funcdesc}
143
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144\begin{funcdesc}{lower}{s}
145Convert letters to lower case.
146\end{funcdesc}
147
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000148\begin{funcdesc}{maketrans}{from, to}
149Return a translation table suitable for passing to \code{string.translate}
150or \code{regex.compile}, that will map each character in \var{from}
151into the character at the same position in \var{to}; \var{from} and
152\var{to} must have the same length.
153\end{funcdesc}
154
Guido van Rossume5e55d71996-08-09 21:44:51 +0000155\begin{funcdesc}{split}{s\optional{\, sep\optional{\, maxsplit}}}
156Return a list of the words of the string \var{s}. If the optional
157second argument \var{sep} is absent or \code{None}, the words are
158separated by arbitrary strings of whitespace characters (space, tab,
159newline, return, formfeed). If the second argument \var{sep} is
160present and not \code{None}, it specifies a string to be used as the
161word separator. The returned list will then have one more items than
162the number of non-overlapping occurrences of the separator in the
163string. The optional third argument \var{maxsplit} defaults to 0. If
164it is nonzero, at most \var{maxsplit} number of splits occur, and the
165remainder of the string is returned as the final element of the list
166(thus, the list will have at most \code{\var{maxsplit}+1} elements).
167(See also \code{regsub.split()} for a version that allows specifying a
168regular expression as the separator.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000169\end{funcdesc}
170
Guido van Rossume5e55d71996-08-09 21:44:51 +0000171\begin{funcdesc}{splitfields}{s\optional{\, sep\optional{\, maxsplit}}}
Guido van Rossum0fa066b1997-06-02 17:30:20 +0000172This function behaves identically to \code{split}. (In the past,
Guido van Rossume5e55d71996-08-09 21:44:51 +0000173\code{split} was only used with one argument, while \code{splitfields}
174was only used with two arguments.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000175\end{funcdesc}
176
Guido van Rossume5e55d71996-08-09 21:44:51 +0000177\begin{funcdesc}{join}{words\optional{\, sep}}
178Concatenate a list or tuple of words with intervening occurrences of
179\var{sep}. The default value for \var{sep} is a single space character.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000180It is always true that
Guido van Rossume5e55d71996-08-09 21:44:51 +0000181\code{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
182equals \var{s}.
183\end{funcdesc}
184
185\begin{funcdesc}{joinfields}{words\optional{\, sep}}
186This function behaves identical to \code{join}. (In the past,
187\code{join} was only used with one argument, while \code{joinfields}
188was only used with two arguments.)
189\end{funcdesc}
190
191\begin{funcdesc}{lstrip}{s}
192Remove leading whitespace from the string \var{s}.
193\end{funcdesc}
194
195\begin{funcdesc}{rstrip}{s}
196Remove trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000197\end{funcdesc}
198
199\begin{funcdesc}{strip}{s}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000200Remove leading and trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000201\end{funcdesc}
202
203\begin{funcdesc}{swapcase}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000204Convert lower case letters to upper case and vice versa.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205\end{funcdesc}
206
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000207\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
208Delete all characters from \var{s} that are in \var{deletechars} (if present), and
209then translate the characters using \var{table}, which must be
Guido van Rossumf65f2781995-09-13 17:37:21 +0000210a 256-character string giving the translation for each character
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000211value, indexed by its ordinal.
Guido van Rossumf65f2781995-09-13 17:37:21 +0000212\end{funcdesc}
213
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000214\begin{funcdesc}{upper}{s}
215Convert letters to upper case.
216\end{funcdesc}
217
218\begin{funcdesc}{ljust}{s\, width}
219\funcline{rjust}{s\, width}
220\funcline{center}{s\, width}
221These functions respectively left-justify, right-justify and center a
222string in a field of given width.
223They return a string that is at least
224\var{width}
225characters wide, created by padding the string
226\var{s}
227with spaces until the given width on the right, left or both sides.
228The string is never truncated.
229\end{funcdesc}
230
231\begin{funcdesc}{zfill}{s\, width}
232Pad a numeric string on the left with zero digits until the given
233width is reached. Strings starting with a sign are handled correctly.
234\end{funcdesc}
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000235
Guido van Rossum740eb821997-04-02 05:56:16 +0000236\begin{funcdesc}{replace}{str, old, new\optional{, maxsplit}}
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000237Return a copy of string \var{str} with all occurrences of substring
Guido van Rossum740eb821997-04-02 05:56:16 +0000238\var{old} replaced by \var{new}. If the optional argument
239\var{maxsplit} is given, the first \var{maxsplit} occurrences are
240replaced.
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000241\end{funcdesc}
242
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000243This module is implemented in Python. Much of its functionality has
244been reimplemented in the built-in module \code{strop}. However, you
245should \emph{never} import the latter module directly. When
246\code{string} discovers that \code{strop} exists, it transparently
247replaces parts of itself with the implementation from \code{strop}.
248After initialization, there is \emph{no} overhead in using
249\code{string} instead of \code{strop}.
250\bimodindex{strop}