blob: 7d6f4bdaae09e8679b8303ebb6c872f2a80905e0 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{string}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-string}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\stmodindex{string}
4
5This module defines some constants useful for checking character
Fred Drake6d2bdb61997-12-16 04:04:25 +00006classes and some useful string functions. See the module
Fred Drakecce10901998-03-17 06:33:25 +00007\module{re}\refstmodindex{re} for string functions based on regular
8expressions.
Guido van Rossum0bf4d891995-03-02 12:37:30 +00009
10The constants defined in this module are are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012\begin{datadesc}{digits}
13 The string \code{'0123456789'}.
14\end{datadesc}
15
16\begin{datadesc}{hexdigits}
17 The string \code{'0123456789abcdefABCDEF'}.
18\end{datadesc}
19
20\begin{datadesc}{letters}
Fred Drakecce10901998-03-17 06:33:25 +000021 The concatenation of the strings \function{lowercase()} and
22 \function{uppercase()} described below.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023\end{datadesc}
24
25\begin{datadesc}{lowercase}
26 A string containing all the characters that are considered lowercase
27 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000028 \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000029 the effect on the routines \function{upper()} and
30 \function{swapcase()} is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031\end{datadesc}
32
33\begin{datadesc}{octdigits}
34 The string \code{'01234567'}.
35\end{datadesc}
36
37\begin{datadesc}{uppercase}
38 A string containing all the characters that are considered uppercase
39 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000040 \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000041 the effect on the routines \function{lower()} and
42 \function{swapcase()} is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000043\end{datadesc}
44
45\begin{datadesc}{whitespace}
46 A string containing all characters that are considered whitespace.
47 On most systems this includes the characters space, tab, linefeed,
Guido van Rossum86751151995-02-28 17:14:32 +000048 return, formfeed, and vertical tab. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000049 the effect on the routines \function{strip()} and \function{split()}
50 is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051\end{datadesc}
52
Guido van Rossum0bf4d891995-03-02 12:37:30 +000053The functions defined in this module are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055
56\begin{funcdesc}{atof}{s}
57Convert a string to a floating point number. The string must have
58the standard syntax for a floating point literal in Python, optionally
Guido van Rossum740eb821997-04-02 05:56:16 +000059preceded by a sign (\samp{+} or \samp{-}). Note that this behaves
Fred Drakecce10901998-03-17 06:33:25 +000060identical to the built-in function
61\function{float()}\bifuncindex{float} when passed a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062\end{funcdesc}
63
Fred Drakecce10901998-03-17 06:33:25 +000064\begin{funcdesc}{atoi}{s\optional{, base}}
Guido van Rossum470be141995-03-17 16:07:09 +000065Convert string \var{s} to an integer in the given \var{base}. The
66string must consist of one or more digits, optionally preceded by a
67sign (\samp{+} or \samp{-}). The \var{base} defaults to 10. If it is
680, a default base is chosen depending on the leading characters of the
69string (after stripping the sign): \samp{0x} or \samp{0X} means 16,
70\samp{0} means 8, anything else means 10. If \var{base} is 16, a
Guido van Rossum740eb821997-04-02 05:56:16 +000071leading \samp{0x} or \samp{0X} is always accepted. Note that when
72invoked without \var{base} or with \var{base} set to 10, this behaves
Fred Drakecce10901998-03-17 06:33:25 +000073identical to the built-in function \function{int()} when passed a string.
Guido van Rossum740eb821997-04-02 05:56:16 +000074(Also note: for a more flexible interpretation of numeric literals,
Fred Drakecce10901998-03-17 06:33:25 +000075use the built-in function \function{eval()}\bifuncindex{eval}.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076\end{funcdesc}
77
Fred Drakecce10901998-03-17 06:33:25 +000078\begin{funcdesc}{atol}{s\optional{, base}}
79Convert string \var{s} to a long integer in the given \var{base}. The
Guido van Rossum470be141995-03-17 16:07:09 +000080string must consist of one or more digits, optionally preceded by a
81sign (\samp{+} or \samp{-}). The \var{base} argument has the same
Fred Drakecce10901998-03-17 06:33:25 +000082meaning as for \function{atoi()}. A trailing \samp{l} or \samp{L} is
83not allowed, except if the base is 0. Note that when invoked without
Guido van Rossum740eb821997-04-02 05:56:16 +000084\var{base} or with \var{base} set to 10, this behaves identical to the
Fred Drakecce10901998-03-17 06:33:25 +000085built-in function \function{long()}\bifuncindex{long} when passed a
86string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000087\end{funcdesc}
88
Guido van Rossume5e55d71996-08-09 21:44:51 +000089\begin{funcdesc}{capitalize}{word}
90Capitalize the first character of the argument.
91\end{funcdesc}
92
93\begin{funcdesc}{capwords}{s}
Fred Drakecce10901998-03-17 06:33:25 +000094Split the argument into words using \function{split()}, capitalize
95each word using \function{capitalize()}, and join the capitalized
96words using \function{join()}. Note that this replaces runs of
97whitespace characters by a single space, and removes leading and
98trailing whitespace.
Guido van Rossume5e55d71996-08-09 21:44:51 +000099\end{funcdesc}
100
Fred Drakecce10901998-03-17 06:33:25 +0000101\begin{funcdesc}{expandtabs}{s, tabsize}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000102Expand tabs in a string, i.e.\ replace them by one or more spaces,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000103depending on the current column and the given tab size. The column
104number is reset to zero after each newline occurring in the string.
105This doesn't understand other non-printing characters or escape
106sequences.
107\end{funcdesc}
108
Fred Drakecce10901998-03-17 06:33:25 +0000109\begin{funcdesc}{find}{s, sub\optional{, start\optional{,end}}}
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000110Return the lowest index in \var{s} where the substring \var{sub} is
111found such that \var{sub} is wholly contained in
Fred Drakecce10901998-03-17 06:33:25 +0000112\code{\var{s}[\var{start}:\var{end}]}. Return \code{-1} on failure.
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000113Defaults for \var{start} and \var{end} and interpretation of negative
114values is the same as for slices.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000115\end{funcdesc}
116
Fred Drakecce10901998-03-17 06:33:25 +0000117\begin{funcdesc}{rfind}{s, sub\optional{, start\optional{, end}}}
118Like \function{find()} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000119\end{funcdesc}
120
Fred Drakecce10901998-03-17 06:33:25 +0000121\begin{funcdesc}{index}{s, sub\optional{, start\optional{, end}}}
122Like \function{find()} but raise \exception{ValueError} when the
123substring is not found.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124\end{funcdesc}
125
Fred Drakecce10901998-03-17 06:33:25 +0000126\begin{funcdesc}{rindex}{s, sub\optional{, start\optional{, end}}}
127Like \function{rfind()} but raise \exception{ValueError} when the
128substring is not found.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000129\end{funcdesc}
130
Fred Drakecce10901998-03-17 06:33:25 +0000131\begin{funcdesc}{count}{s, sub\optional{, start\optional{, end}}}
Guido van Rossumab3a2501994-08-01 12:18:36 +0000132Return the number of (non-overlapping) occurrences of substring
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000133\var{sub} in string \code{\var{s}[\var{start}:\var{end}]}.
134Defaults for \var{start} and \var{end} and interpretation of negative
135values is the same as for slices.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000136\end{funcdesc}
137
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000138\begin{funcdesc}{lower}{s}
139Convert letters to lower case.
140\end{funcdesc}
141
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000142\begin{funcdesc}{maketrans}{from, to}
Fred Drakecce10901998-03-17 06:33:25 +0000143Return a translation table suitable for passing to
144\function{translate()} or \function{regex.compile()}, that will map
145each character in \var{from} into the character at the same position
146in \var{to}; \var{from} and \var{to} must have the same length.
Guido van Rossuma3eebe61998-06-11 16:03:30 +0000147
148\strong{Warning:} don't use strings derived from \code{lowercase} and
149\code{uppercase} as arguments; in some locales, these don't have the
150same length. For case conversions, always use \function{lower()} and
151\function{upper()}.
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000152\end{funcdesc}
153
Fred Drakecce10901998-03-17 06:33:25 +0000154\begin{funcdesc}{split}{s\optional{, sep\optional{, maxsplit}}}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000155Return 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).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000166\end{funcdesc}
167
Fred Drakecce10901998-03-17 06:33:25 +0000168\begin{funcdesc}{splitfields}{s\optional{, sep\optional{, maxsplit}}}
169This function behaves identically to \function{split()}. (In the
170past, \function{split()} was only used with one argument, while
171\function{splitfields()} was only used with two arguments.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000172\end{funcdesc}
173
Fred Drakecce10901998-03-17 06:33:25 +0000174\begin{funcdesc}{join}{words\optional{, sep}}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000175Concatenate a list or tuple of words with intervening occurrences of
Fred Drakecce10901998-03-17 06:33:25 +0000176\var{sep}. The default value for \var{sep} is a single space
177character. It is always true that
178\samp{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000179equals \var{s}.
180\end{funcdesc}
181
Fred Drakecce10901998-03-17 06:33:25 +0000182\begin{funcdesc}{joinfields}{words\optional{, sep}}
183This function behaves identical to \function{join()}. (In the past,
184\function{join()} was only used with one argument, while
185\function{joinfields()} was only used with two arguments.)
Guido van Rossume5e55d71996-08-09 21:44:51 +0000186\end{funcdesc}
187
188\begin{funcdesc}{lstrip}{s}
189Remove leading whitespace from the string \var{s}.
190\end{funcdesc}
191
192\begin{funcdesc}{rstrip}{s}
193Remove trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194\end{funcdesc}
195
196\begin{funcdesc}{strip}{s}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000197Remove leading and trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000198\end{funcdesc}
199
200\begin{funcdesc}{swapcase}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000201Convert lower case letters to upper case and vice versa.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000202\end{funcdesc}
203
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000204\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
Fred Drakecce10901998-03-17 06:33:25 +0000205Delete all characters from \var{s} that are in \var{deletechars} (if
206present), and then translate the characters using \var{table}, which
207must be a 256-character string giving the translation for each
208character value, indexed by its ordinal.
Guido van Rossumf65f2781995-09-13 17:37:21 +0000209\end{funcdesc}
210
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000211\begin{funcdesc}{upper}{s}
212Convert letters to upper case.
213\end{funcdesc}
214
Fred Drakecce10901998-03-17 06:33:25 +0000215\begin{funcdesc}{ljust}{s, width}
216\funcline{rjust}{s, width}
217\funcline{center}{s, width}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000218These functions respectively left-justify, right-justify and center a
219string in a field of given width.
220They return a string that is at least
221\var{width}
222characters wide, created by padding the string
223\var{s}
224with spaces until the given width on the right, left or both sides.
225The string is never truncated.
226\end{funcdesc}
227
Fred Drakecce10901998-03-17 06:33:25 +0000228\begin{funcdesc}{zfill}{s, width}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000229Pad a numeric string on the left with zero digits until the given
230width is reached. Strings starting with a sign are handled correctly.
231\end{funcdesc}
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000232
Guido van Rossum740eb821997-04-02 05:56:16 +0000233\begin{funcdesc}{replace}{str, old, new\optional{, maxsplit}}
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000234Return a copy of string \var{str} with all occurrences of substring
Guido van Rossum740eb821997-04-02 05:56:16 +0000235\var{old} replaced by \var{new}. If the optional argument
236\var{maxsplit} is given, the first \var{maxsplit} occurrences are
237replaced.
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000238\end{funcdesc}
239
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000240This module is implemented in Python. Much of its functionality has
Fred Drakecce10901998-03-17 06:33:25 +0000241been reimplemented in the built-in module
242\module{strop}\refbimodindex{strop}. However, you
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000243should \emph{never} import the latter module directly. When
Fred Drakecce10901998-03-17 06:33:25 +0000244\module{string} discovers that \module{strop} exists, it transparently
245replaces parts of itself with the implementation from \module{strop}.
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000246After initialization, there is \emph{no} overhead in using
Fred Drakecce10901998-03-17 06:33:25 +0000247\module{string} instead of \module{strop}.