blob: 7628a10ab2b51765515fcc61847d2c2f44995f36 [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
Guido van Rossum86751151995-02-28 17:14:32 +000026 \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000027 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
Guido van Rossum86751151995-02-28 17:14:32 +000038 \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039 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,
Guido van Rossum86751151995-02-28 17:14:32 +000046 return, formfeed, and vertical tab. Do not change its definition ---
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047 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
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055The functions are:
56
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
62preceded by a sign (\samp{+} or \samp{-}).
63\end{funcdesc}
64
65\begin{funcdesc}{atoi}{s}
66Convert a string to an integer. The string must consist of one or more
67digits, optionally preceded by a sign (\samp{+} or \samp{-}).
68\end{funcdesc}
69
70\begin{funcdesc}{atol}{s}
71Convert a string to a long integer. The string must consist of one
72or more digits, optionally preceded by a sign (\samp{+} or \samp{-}).
73\end{funcdesc}
74
75\begin{funcdesc}{expandtabs}{s\, tabsize}
76Expand tabs in a string, i.e. replace them by one or more spaces,
77depending on the current column and the given tab size. The column
78number is reset to zero after each newline occurring in the string.
79This doesn't understand other non-printing characters or escape
80sequences.
81\end{funcdesc}
82
Guido van Rossum16d6e711994-08-08 12:30:22 +000083\begin{funcdesc}{find}{s\, sub\optional{\, start}}
84Return the lowest index in \var{s} not smaller than \var{start} where the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000085substring \var{sub} is found. Return \code{-1} when \var{sub}
Guido van Rossum16d6e711994-08-08 12:30:22 +000086does not occur as a substring of \var{s} with index at least \var{start}.
87If \var{start} is omitted, it defaults to \code{0}. If \var{start} is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088negative, \code{len(\var{s})} is added.
89\end{funcdesc}
90
Guido van Rossum16d6e711994-08-08 12:30:22 +000091\begin{funcdesc}{rfind}{s\, sub\optional{\, start}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092Like \code{find} but finds the highest index.
93\end{funcdesc}
94
Guido van Rossum16d6e711994-08-08 12:30:22 +000095\begin{funcdesc}{index}{s\, sub\optional{\, start}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +000096Like \code{find} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000097not found.
98\end{funcdesc}
99
Guido van Rossum16d6e711994-08-08 12:30:22 +0000100\begin{funcdesc}{rindex}{s\, sub\optional{\, start}}
Guido van Rossum2828e9d1994-08-17 13:16:34 +0000101Like \code{rfind} but raise \code{ValueError} when the substring is
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000102not found.
103\end{funcdesc}
104
Guido van Rossumab3a2501994-08-01 12:18:36 +0000105\begin{funcdesc}{count}{s\, sub\, i}
106Return the number of (non-overlapping) occurrences of substring
107\var{sub} in string \var{s} with index at least \var{i}.
108If \var{i} is omitted, it defaults to \code{0}.
109\end{funcdesc}
110
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000111\begin{funcdesc}{lower}{s}
112Convert letters to lower case.
113\end{funcdesc}
114
115\begin{funcdesc}{split}{s}
116Returns a list of the whitespace-delimited words of the string
117\var{s}.
118\end{funcdesc}
119
120\begin{funcdesc}{splitfields}{s\, sep}
121 Returns a list containing the fields of the string \var{s}, using
122 the string \var{sep} as a separator. The list will have one more
123 items than the number of non-overlapping occurrences of the
124 separator in the string. Thus, \code{string.splitfields(\var{s}, '
125 ')} is not the same as \code{string.split(\var{s})}, as the latter
126 only returns non-empty words. As a special case,
127 \code{splitfields(\var{s}, '')} returns \code{[\var{s}]}, for any string
128 \var{s}. (See also \code{regsub.split()}.)
129\end{funcdesc}
130
131\begin{funcdesc}{join}{words}
132Concatenate a list or tuple of words with intervening spaces.
133\end{funcdesc}
134
135\begin{funcdesc}{joinfields}{words\, sep}
136Concatenate a list or tuple of words with intervening separators.
137It is always true that
138\code{string.joinfields(string.splitfields(\var{t}, \var{sep}), \var{sep})}
139equals \var{t}.
140\end{funcdesc}
141
142\begin{funcdesc}{strip}{s}
143Removes leading and trailing whitespace from the string
144\var{s}.
145\end{funcdesc}
146
147\begin{funcdesc}{swapcase}{s}
148Converts lower case letters to upper case and vice versa.
149\end{funcdesc}
150
151\begin{funcdesc}{upper}{s}
152Convert letters to upper case.
153\end{funcdesc}
154
155\begin{funcdesc}{ljust}{s\, width}
156\funcline{rjust}{s\, width}
157\funcline{center}{s\, width}
158These functions respectively left-justify, right-justify and center a
159string in a field of given width.
160They return a string that is at least
161\var{width}
162characters wide, created by padding the string
163\var{s}
164with spaces until the given width on the right, left or both sides.
165The string is never truncated.
166\end{funcdesc}
167
168\begin{funcdesc}{zfill}{s\, width}
169Pad a numeric string on the left with zero digits until the given
170width is reached. Strings starting with a sign are handled correctly.
171\end{funcdesc}