blob: f7189fdc4dcb49d7be6e5639009981f2752cae8c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{string} ---
2 Common string operations.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{string}
4
5\modulesynopsis{Common string operations.}
6
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
8This module defines some constants useful for checking character
Fred Drake6d2bdb61997-12-16 04:04:25 +00009classes and some useful string functions. See the module
Fred Drakecce10901998-03-17 06:33:25 +000010\module{re}\refstmodindex{re} for string functions based on regular
11expressions.
Guido van Rossum0bf4d891995-03-02 12:37:30 +000012
13The constants defined in this module are are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015\begin{datadesc}{digits}
16 The string \code{'0123456789'}.
17\end{datadesc}
18
19\begin{datadesc}{hexdigits}
20 The string \code{'0123456789abcdefABCDEF'}.
21\end{datadesc}
22
23\begin{datadesc}{letters}
Fred Drakecce10901998-03-17 06:33:25 +000024 The concatenation of the strings \function{lowercase()} and
25 \function{uppercase()} described below.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026\end{datadesc}
27
28\begin{datadesc}{lowercase}
29 A string containing all the characters that are considered lowercase
30 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000031 \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000032 the effect on the routines \function{upper()} and
33 \function{swapcase()} is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034\end{datadesc}
35
36\begin{datadesc}{octdigits}
37 The string \code{'01234567'}.
38\end{datadesc}
39
40\begin{datadesc}{uppercase}
41 A string containing all the characters that are considered uppercase
42 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000043 \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000044 the effect on the routines \function{lower()} and
45 \function{swapcase()} is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046\end{datadesc}
47
48\begin{datadesc}{whitespace}
49 A string containing all characters that are considered whitespace.
50 On most systems this includes the characters space, tab, linefeed,
Guido van Rossum86751151995-02-28 17:14:32 +000051 return, formfeed, and vertical tab. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000052 the effect on the routines \function{strip()} and \function{split()}
53 is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054\end{datadesc}
55
Guido van Rossum0bf4d891995-03-02 12:37:30 +000056The functions defined in this module are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058
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
Fred Drakecce10901998-03-17 06:33:25 +000063identical to the built-in function
64\function{float()}\bifuncindex{float} when passed a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065\end{funcdesc}
66
Fred Drakecce10901998-03-17 06:33:25 +000067\begin{funcdesc}{atoi}{s\optional{, base}}
Guido van Rossum470be141995-03-17 16:07:09 +000068Convert string \var{s} to an integer in the given \var{base}. The
69string must consist of one or more digits, optionally preceded by a
70sign (\samp{+} or \samp{-}). The \var{base} defaults to 10. If it is
710, a default base is chosen depending on the leading characters of the
72string (after stripping the sign): \samp{0x} or \samp{0X} means 16,
73\samp{0} means 8, anything else means 10. If \var{base} is 16, a
Guido van Rossum740eb821997-04-02 05:56:16 +000074leading \samp{0x} or \samp{0X} is always accepted. Note that when
75invoked without \var{base} or with \var{base} set to 10, this behaves
Fred Drakecce10901998-03-17 06:33:25 +000076identical to the built-in function \function{int()} when passed a string.
Guido van Rossum740eb821997-04-02 05:56:16 +000077(Also note: for a more flexible interpretation of numeric literals,
Fred Drakecce10901998-03-17 06:33:25 +000078use the built-in function \function{eval()}\bifuncindex{eval}.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\end{funcdesc}
80
Fred Drakecce10901998-03-17 06:33:25 +000081\begin{funcdesc}{atol}{s\optional{, base}}
82Convert string \var{s} to a long integer in the given \var{base}. The
Guido van Rossum470be141995-03-17 16:07:09 +000083string must consist of one or more digits, optionally preceded by a
84sign (\samp{+} or \samp{-}). The \var{base} argument has the same
Fred Drakecce10901998-03-17 06:33:25 +000085meaning as for \function{atoi()}. A trailing \samp{l} or \samp{L} is
86not allowed, except if the base is 0. Note that when invoked without
Guido van Rossum740eb821997-04-02 05:56:16 +000087\var{base} or with \var{base} set to 10, this behaves identical to the
Fred Drakecce10901998-03-17 06:33:25 +000088built-in function \function{long()}\bifuncindex{long} when passed a
89string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000090\end{funcdesc}
91
Guido van Rossume5e55d71996-08-09 21:44:51 +000092\begin{funcdesc}{capitalize}{word}
93Capitalize the first character of the argument.
94\end{funcdesc}
95
96\begin{funcdesc}{capwords}{s}
Fred Drakecce10901998-03-17 06:33:25 +000097Split the argument into words using \function{split()}, capitalize
98each word using \function{capitalize()}, and join the capitalized
99words using \function{join()}. Note that this replaces runs of
100whitespace characters by a single space, and removes leading and
101trailing whitespace.
Guido van Rossume5e55d71996-08-09 21:44:51 +0000102\end{funcdesc}
103
Fred Drakecce10901998-03-17 06:33:25 +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
Fred Drakecce10901998-03-17 06:33:25 +0000112\begin{funcdesc}{find}{s, sub\optional{, start\optional{,end}}}
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000113Return the lowest index in \var{s} where the substring \var{sub} is
114found such that \var{sub} is wholly contained in
Fred Drakecce10901998-03-17 06:33:25 +0000115\code{\var{s}[\var{start}:\var{end}]}. Return \code{-1} on failure.
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000116Defaults for \var{start} and \var{end} and interpretation of negative
117values is the same as for slices.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000118\end{funcdesc}
119
Fred Drakecce10901998-03-17 06:33:25 +0000120\begin{funcdesc}{rfind}{s, sub\optional{, start\optional{, end}}}
121Like \function{find()} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000122\end{funcdesc}
123
Fred Drakecce10901998-03-17 06:33:25 +0000124\begin{funcdesc}{index}{s, sub\optional{, start\optional{, end}}}
125Like \function{find()} but raise \exception{ValueError} when the
126substring is not found.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000127\end{funcdesc}
128
Fred Drakecce10901998-03-17 06:33:25 +0000129\begin{funcdesc}{rindex}{s, sub\optional{, start\optional{, end}}}
130Like \function{rfind()} but raise \exception{ValueError} when the
131substring is not found.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132\end{funcdesc}
133
Fred Drakecce10901998-03-17 06:33:25 +0000134\begin{funcdesc}{count}{s, sub\optional{, start\optional{, end}}}
Guido van Rossumab3a2501994-08-01 12:18:36 +0000135Return the number of (non-overlapping) occurrences of substring
Guido van Rossum828a0bd1997-10-20 22:40:26 +0000136\var{sub} in string \code{\var{s}[\var{start}:\var{end}]}.
137Defaults for \var{start} and \var{end} and interpretation of negative
138values is the same as for slices.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000139\end{funcdesc}
140
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000141\begin{funcdesc}{lower}{s}
142Convert letters to lower case.
143\end{funcdesc}
144
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000145\begin{funcdesc}{maketrans}{from, to}
Fred Drakecce10901998-03-17 06:33:25 +0000146Return a translation table suitable for passing to
147\function{translate()} or \function{regex.compile()}, that will map
148each character in \var{from} into the character at the same position
149in \var{to}; \var{from} and \var{to} must have the same length.
Guido van Rossuma3eebe61998-06-11 16:03:30 +0000150
151\strong{Warning:} don't use strings derived from \code{lowercase} and
152\code{uppercase} as arguments; in some locales, these don't have the
153same length. For case conversions, always use \function{lower()} and
154\function{upper()}.
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000155\end{funcdesc}
156
Fred Drakecce10901998-03-17 06:33:25 +0000157\begin{funcdesc}{split}{s\optional{, sep\optional{, maxsplit}}}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000158Return a list of the words of the string \var{s}. If the optional
159second argument \var{sep} is absent or \code{None}, the words are
160separated by arbitrary strings of whitespace characters (space, tab,
161newline, return, formfeed). If the second argument \var{sep} is
162present and not \code{None}, it specifies a string to be used as the
163word separator. The returned list will then have one more items than
164the number of non-overlapping occurrences of the separator in the
165string. The optional third argument \var{maxsplit} defaults to 0. If
166it is nonzero, at most \var{maxsplit} number of splits occur, and the
167remainder of the string is returned as the final element of the list
168(thus, the list will have at most \code{\var{maxsplit}+1} elements).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000169\end{funcdesc}
170
Fred Drakecce10901998-03-17 06:33:25 +0000171\begin{funcdesc}{splitfields}{s\optional{, sep\optional{, maxsplit}}}
172This function behaves identically to \function{split()}. (In the
173past, \function{split()} was only used with one argument, while
174\function{splitfields()} was only used with two arguments.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000175\end{funcdesc}
176
Fred Drakecce10901998-03-17 06:33:25 +0000177\begin{funcdesc}{join}{words\optional{, sep}}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000178Concatenate a list or tuple of words with intervening occurrences of
Fred Drakecce10901998-03-17 06:33:25 +0000179\var{sep}. The default value for \var{sep} is a single space
180character. It is always true that
181\samp{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000182equals \var{s}.
183\end{funcdesc}
184
Fred Drakecce10901998-03-17 06:33:25 +0000185\begin{funcdesc}{joinfields}{words\optional{, sep}}
186This function behaves identical to \function{join()}. (In the past,
187\function{join()} was only used with one argument, while
188\function{joinfields()} was only used with two arguments.)
Guido van Rossume5e55d71996-08-09 21:44:51 +0000189\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}}
Fred Drakecce10901998-03-17 06:33:25 +0000208Delete all characters from \var{s} that are in \var{deletechars} (if
209present), and then translate the characters using \var{table}, which
210must be a 256-character string giving the translation for each
211character value, 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
Fred Drakecce10901998-03-17 06:33:25 +0000218\begin{funcdesc}{ljust}{s, width}
219\funcline{rjust}{s, width}
220\funcline{center}{s, width}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000221These 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
Fred Drakecce10901998-03-17 06:33:25 +0000231\begin{funcdesc}{zfill}{s, width}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000232Pad 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
Fred Drakecce10901998-03-17 06:33:25 +0000244been reimplemented in the built-in module
245\module{strop}\refbimodindex{strop}. However, you
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000246should \emph{never} import the latter module directly. When
Fred Drakecce10901998-03-17 06:33:25 +0000247\module{string} discovers that \module{strop} exists, it transparently
248replaces parts of itself with the implementation from \module{strop}.
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000249After initialization, there is \emph{no} overhead in using
Fred Drakecce10901998-03-17 06:33:25 +0000250\module{string} instead of \module{strop}.