blob: 7d99a360a997e58923956bc1f224ed22bad1ad64 [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
6classes, some exceptions, and some useful string functions.
7The constants are:
8
9\renewcommand{\indexsubitem}{(data in module string)}
10\begin{datadesc}{digits}
11 The string \code{'0123456789'}.
12\end{datadesc}
13
14\begin{datadesc}{hexdigits}
15 The string \code{'0123456789abcdefABCDEF'}.
16\end{datadesc}
17
18\begin{datadesc}{letters}
19 The concatenation of the strings \code{lowercase} and
20 \code{uppercase} described below.
21\end{datadesc}
22
23\begin{datadesc}{lowercase}
24 A string containing all the characters that are considered lowercase
25 letters. On most systems this is the string
26 \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition --
27 the effect on the routines \code{upper} and \code{swapcase} is
28 undefined.
29\end{datadesc}
30
31\begin{datadesc}{octdigits}
32 The string \code{'01234567'}.
33\end{datadesc}
34
35\begin{datadesc}{uppercase}
36 A string containing all the characters that are considered uppercase
37 letters. On most systems this is the string
38 \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition --
39 the effect on the routines \code{lower} and \code{swapcase} is
40 undefined.
41\end{datadesc}
42
43\begin{datadesc}{whitespace}
44 A string containing all characters that are considered whitespace.
45 On most systems this includes the characters space, tab, linefeed,
46 return, formfeed, and vertical tab. Do not change its definition --
47 the effect on the routines \code{strip} and \code{split} is
48 undefined.
49\end{datadesc}
50
51The exceptions are:
52
53\renewcommand{\indexsubitem}{(exception in module string)}
54
55\begin{excdesc}{atof_error}
56Exception raised by
57\code{atof}
58when a non-float string argument is detected.
59The exception argument is the offending string.
60\end{excdesc}
61
62\begin{excdesc}{atoi_error}
63Exception raised by
64\code{atoi}
65when a non-integer string argument is detected.
66The exception argument is the offending string.
67\end{excdesc}
68
69\begin{excdesc}{atol_error}
70Exception raised by
71\code{atol}
72when a non-integer string argument is detected.
73The exception argument is the offending string.
74\end{excdesc}
75
76\begin{excdesc}{index_error}
77Exception raised by \code{index} when \var{sub} is not found.
78The exception argument is undefined (it may be a tuple containing the
79offending arguments to \code{index} or it may be the constant string
80\code{'substring not found'}).
81\end{excdesc}
82
83The functions are:
84
85\renewcommand{\indexsubitem}{(in module string)}
86
87\begin{funcdesc}{atof}{s}
88Convert a string to a floating point number. The string must have
89the standard syntax for a floating point literal in Python, optionally
90preceded by a sign (\samp{+} or \samp{-}).
91\end{funcdesc}
92
93\begin{funcdesc}{atoi}{s}
94Convert a string to an integer. The string must consist of one or more
95digits, optionally preceded by a sign (\samp{+} or \samp{-}).
96\end{funcdesc}
97
98\begin{funcdesc}{atol}{s}
99Convert a string to a long integer. The string must consist of one
100or more digits, optionally preceded by a sign (\samp{+} or \samp{-}).
101\end{funcdesc}
102
103\begin{funcdesc}{expandtabs}{s\, tabsize}
104Expand tabs in a string, i.e. replace them by one or more spaces,
105depending 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 Rossum16d6e711994-08-08 12:30:22 +0000111\begin{funcdesc}{find}{s\, sub\optional{\, start}}
112Return the lowest index in \var{s} not smaller than \var{start} where the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000113substring \var{sub} is found. Return \code{-1} when \var{sub}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000114does not occur as a substring of \var{s} with index at least \var{start}.
115If \var{start} is omitted, it defaults to \code{0}. If \var{start} is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000116negative, \code{len(\var{s})} is added.
117\end{funcdesc}
118
Guido van Rossum16d6e711994-08-08 12:30:22 +0000119\begin{funcdesc}{rfind}{s\, sub\optional{\, start}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120Like \code{find} but finds the highest index.
121\end{funcdesc}
122
Guido van Rossum16d6e711994-08-08 12:30:22 +0000123\begin{funcdesc}{index}{s\, sub\optional{\, start}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124Like \code{find} but raise \code{index_error} when the substring is
125not found.
126\end{funcdesc}
127
Guido van Rossum16d6e711994-08-08 12:30:22 +0000128\begin{funcdesc}{rindex}{s\, sub\optional{\, start}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000129Like \code{rfind} but raise \code{index_error} when the substring is
130not found.
131\end{funcdesc}
132
Guido van Rossumab3a2501994-08-01 12:18:36 +0000133\begin{funcdesc}{count}{s\, sub\, i}
134Return the number of (non-overlapping) occurrences of substring
135\var{sub} in string \var{s} with index at least \var{i}.
136If \var{i} is omitted, it defaults to \code{0}.
137\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
143\begin{funcdesc}{split}{s}
144Returns a list of the whitespace-delimited words of the string
145\var{s}.
146\end{funcdesc}
147
148\begin{funcdesc}{splitfields}{s\, sep}
149 Returns a list containing the fields of the string \var{s}, using
150 the string \var{sep} as a separator. The list will have one more
151 items than the number of non-overlapping occurrences of the
152 separator in the string. Thus, \code{string.splitfields(\var{s}, '
153 ')} is not the same as \code{string.split(\var{s})}, as the latter
154 only returns non-empty words. As a special case,
155 \code{splitfields(\var{s}, '')} returns \code{[\var{s}]}, for any string
156 \var{s}. (See also \code{regsub.split()}.)
157\end{funcdesc}
158
159\begin{funcdesc}{join}{words}
160Concatenate a list or tuple of words with intervening spaces.
161\end{funcdesc}
162
163\begin{funcdesc}{joinfields}{words\, sep}
164Concatenate a list or tuple of words with intervening separators.
165It is always true that
166\code{string.joinfields(string.splitfields(\var{t}, \var{sep}), \var{sep})}
167equals \var{t}.
168\end{funcdesc}
169
170\begin{funcdesc}{strip}{s}
171Removes leading and trailing whitespace from the string
172\var{s}.
173\end{funcdesc}
174
175\begin{funcdesc}{swapcase}{s}
176Converts lower case letters to upper case and vice versa.
177\end{funcdesc}
178
179\begin{funcdesc}{upper}{s}
180Convert letters to upper case.
181\end{funcdesc}
182
183\begin{funcdesc}{ljust}{s\, width}
184\funcline{rjust}{s\, width}
185\funcline{center}{s\, width}
186These functions respectively left-justify, right-justify and center a
187string in a field of given width.
188They return a string that is at least
189\var{width}
190characters wide, created by padding the string
191\var{s}
192with spaces until the given width on the right, left or both sides.
193The string is never truncated.
194\end{funcdesc}
195
196\begin{funcdesc}{zfill}{s\, width}
197Pad a numeric string on the left with zero digits until the given
198width is reached. Strings starting with a sign are handled correctly.
199\end{funcdesc}