blob: c7889c950a0a8092003ba3b535ccf59ea9f6efaf [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\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
Fred Drake19479911998-02-13 06:58:54 +000012\setindexsubitem{(data in module string)}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000013\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}
Fred Drakecce10901998-03-17 06:33:25 +000022 The concatenation of the strings \function{lowercase()} and
23 \function{uppercase()} described below.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024\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 ---
Fred Drakecce10901998-03-17 06:33:25 +000030 the effect on the routines \function{upper()} and
31 \function{swapcase()} is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032\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 ---
Fred Drakecce10901998-03-17 06:33:25 +000042 the effect on the routines \function{lower()} and
43 \function{swapcase()} is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044\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 ---
Fred Drakecce10901998-03-17 06:33:25 +000050 the effect on the routines \function{strip()} and \function{split()}
51 is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052\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
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056
57\begin{funcdesc}{atof}{s}
58Convert a string to a floating point number. The string must have
59the standard syntax for a floating point literal in Python, optionally
Guido van Rossum740eb821997-04-02 05:56:16 +000060preceded by a sign (\samp{+} or \samp{-}). Note that this behaves
Fred Drakecce10901998-03-17 06:33:25 +000061identical to the built-in function
62\function{float()}\bifuncindex{float} when passed a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063\end{funcdesc}
64
Fred Drakecce10901998-03-17 06:33:25 +000065\begin{funcdesc}{atoi}{s\optional{, base}}
Guido van Rossum470be141995-03-17 16:07:09 +000066Convert 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
Fred Drakecce10901998-03-17 06:33:25 +000074identical to the built-in function \function{int()} when passed a string.
Guido van Rossum740eb821997-04-02 05:56:16 +000075(Also note: for a more flexible interpretation of numeric literals,
Fred Drakecce10901998-03-17 06:33:25 +000076use the built-in function \function{eval()}\bifuncindex{eval}.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000077\end{funcdesc}
78
Fred Drakecce10901998-03-17 06:33:25 +000079\begin{funcdesc}{atol}{s\optional{, base}}
80Convert string \var{s} to a long integer in the given \var{base}. The
Guido van Rossum470be141995-03-17 16:07:09 +000081string must consist of one or more digits, optionally preceded by a
82sign (\samp{+} or \samp{-}). The \var{base} argument has the same
Fred Drakecce10901998-03-17 06:33:25 +000083meaning as for \function{atoi()}. A trailing \samp{l} or \samp{L} is
84not allowed, except if the base is 0. Note that when invoked without
Guido van Rossum740eb821997-04-02 05:56:16 +000085\var{base} or with \var{base} set to 10, this behaves identical to the
Fred Drakecce10901998-03-17 06:33:25 +000086built-in function \function{long()}\bifuncindex{long} when passed a
87string.
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}
Fred Drakecce10901998-03-17 06:33:25 +000095Split the argument into words using \function{split()}, capitalize
96each word using \function{capitalize()}, and join the capitalized
97words using \function{join()}. Note that this replaces runs of
98whitespace characters by a single space, and removes leading and
99trailing whitespace.
Guido van Rossume5e55d71996-08-09 21:44:51 +0000100\end{funcdesc}
101
Fred Drakecce10901998-03-17 06:33:25 +0000102\begin{funcdesc}{expandtabs}{s, tabsize}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000103Expand tabs in a string, i.e.\ replace them by one or more spaces,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104depending on the current column and the given tab size. The column
105number is reset to zero after each newline occurring in the string.
106This doesn't understand other non-printing characters or escape
107sequences.
108\end{funcdesc}
109
Fred Drakecce10901998-03-17 06:33:25 +0000110\begin{funcdesc}{find}{s, sub\optional{, start\optional{,end}}}
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000111Return the lowest index in \var{s} where the substring \var{sub} is
112found such that \var{sub} is wholly contained in
Fred Drakecce10901998-03-17 06:33:25 +0000113\code{\var{s}[\var{start}:\var{end}]}. Return \code{-1} on failure.
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000114Defaults for \var{start} and \var{end} and interpretation of negative
115values is the same as for slices.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000116\end{funcdesc}
117
Fred Drakecce10901998-03-17 06:33:25 +0000118\begin{funcdesc}{rfind}{s, sub\optional{, start\optional{, end}}}
119Like \function{find()} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120\end{funcdesc}
121
Fred Drakecce10901998-03-17 06:33:25 +0000122\begin{funcdesc}{index}{s, sub\optional{, start\optional{, end}}}
123Like \function{find()} but raise \exception{ValueError} when the
124substring is not found.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\end{funcdesc}
126
Fred Drakecce10901998-03-17 06:33:25 +0000127\begin{funcdesc}{rindex}{s, sub\optional{, start\optional{, end}}}
128Like \function{rfind()} but raise \exception{ValueError} when the
129substring is not found.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000130\end{funcdesc}
131
Fred Drakecce10901998-03-17 06:33:25 +0000132\begin{funcdesc}{count}{s, sub\optional{, start\optional{, end}}}
Guido van Rossumab3a2501994-08-01 12:18:36 +0000133Return the number of (non-overlapping) occurrences of substring
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000134\var{sub} in string \code{\var{s}[\var{start}:\var{end}]}.
135Defaults for \var{start} and \var{end} and interpretation of negative
136values is the same as for slices.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000137\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
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000143\begin{funcdesc}{maketrans}{from, to}
Fred Drakecce10901998-03-17 06:33:25 +0000144Return a translation table suitable for passing to
145\function{translate()} or \function{regex.compile()}, that will map
146each character in \var{from} into the character at the same position
147in \var{to}; \var{from} and \var{to} must have the same length.
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000148\end{funcdesc}
149
Fred Drakecce10901998-03-17 06:33:25 +0000150\begin{funcdesc}{split}{s\optional{, sep\optional{, maxsplit}}}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000151Return a list of the words of the string \var{s}. If the optional
152second argument \var{sep} is absent or \code{None}, the words are
153separated by arbitrary strings of whitespace characters (space, tab,
154newline, return, formfeed). If the second argument \var{sep} is
155present and not \code{None}, it specifies a string to be used as the
156word separator. The returned list will then have one more items than
157the number of non-overlapping occurrences of the separator in the
158string. The optional third argument \var{maxsplit} defaults to 0. If
159it is nonzero, at most \var{maxsplit} number of splits occur, and the
160remainder of the string is returned as the final element of the list
161(thus, the list will have at most \code{\var{maxsplit}+1} elements).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000162\end{funcdesc}
163
Fred Drakecce10901998-03-17 06:33:25 +0000164\begin{funcdesc}{splitfields}{s\optional{, sep\optional{, maxsplit}}}
165This function behaves identically to \function{split()}. (In the
166past, \function{split()} was only used with one argument, while
167\function{splitfields()} was only used with two arguments.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000168\end{funcdesc}
169
Fred Drakecce10901998-03-17 06:33:25 +0000170\begin{funcdesc}{join}{words\optional{, sep}}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000171Concatenate a list or tuple of words with intervening occurrences of
Fred Drakecce10901998-03-17 06:33:25 +0000172\var{sep}. The default value for \var{sep} is a single space
173character. It is always true that
174\samp{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000175equals \var{s}.
176\end{funcdesc}
177
Fred Drakecce10901998-03-17 06:33:25 +0000178\begin{funcdesc}{joinfields}{words\optional{, sep}}
179This function behaves identical to \function{join()}. (In the past,
180\function{join()} was only used with one argument, while
181\function{joinfields()} was only used with two arguments.)
Guido van Rossume5e55d71996-08-09 21:44:51 +0000182\end{funcdesc}
183
184\begin{funcdesc}{lstrip}{s}
185Remove leading whitespace from the string \var{s}.
186\end{funcdesc}
187
188\begin{funcdesc}{rstrip}{s}
189Remove trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000190\end{funcdesc}
191
192\begin{funcdesc}{strip}{s}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000193Remove leading and trailing whitespace from the string \var{s}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194\end{funcdesc}
195
196\begin{funcdesc}{swapcase}{s}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000197Convert lower case letters to upper case and vice versa.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000198\end{funcdesc}
199
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000200\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
Fred Drakecce10901998-03-17 06:33:25 +0000201Delete all characters from \var{s} that are in \var{deletechars} (if
202present), and then translate the characters using \var{table}, which
203must be a 256-character string giving the translation for each
204character value, indexed by its ordinal.
Guido van Rossumf65f2781995-09-13 17:37:21 +0000205\end{funcdesc}
206
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000207\begin{funcdesc}{upper}{s}
208Convert letters to upper case.
209\end{funcdesc}
210
Fred Drakecce10901998-03-17 06:33:25 +0000211\begin{funcdesc}{ljust}{s, width}
212\funcline{rjust}{s, width}
213\funcline{center}{s, width}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000214These functions respectively left-justify, right-justify and center a
215string in a field of given width.
216They return a string that is at least
217\var{width}
218characters wide, created by padding the string
219\var{s}
220with spaces until the given width on the right, left or both sides.
221The string is never truncated.
222\end{funcdesc}
223
Fred Drakecce10901998-03-17 06:33:25 +0000224\begin{funcdesc}{zfill}{s, width}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000225Pad a numeric string on the left with zero digits until the given
226width is reached. Strings starting with a sign are handled correctly.
227\end{funcdesc}
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000228
Guido van Rossum740eb821997-04-02 05:56:16 +0000229\begin{funcdesc}{replace}{str, old, new\optional{, maxsplit}}
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000230Return a copy of string \var{str} with all occurrences of substring
Guido van Rossum740eb821997-04-02 05:56:16 +0000231\var{old} replaced by \var{new}. If the optional argument
232\var{maxsplit} is given, the first \var{maxsplit} occurrences are
233replaced.
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000234\end{funcdesc}
235
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000236This module is implemented in Python. Much of its functionality has
Fred Drakecce10901998-03-17 06:33:25 +0000237been reimplemented in the built-in module
238\module{strop}\refbimodindex{strop}. However, you
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000239should \emph{never} import the latter module directly. When
Fred Drakecce10901998-03-17 06:33:25 +0000240\module{string} discovers that \module{strop} exists, it transparently
241replaces parts of itself with the implementation from \module{strop}.
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000242After initialization, there is \emph{no} overhead in using
Fred Drakecce10901998-03-17 06:33:25 +0000243\module{string} instead of \module{strop}.