blob: c9d27b5d5758509319d119e31d38c5778ced133c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{string} ---
Fred Drakeffbe6871999-04-22 21:23:22 +00002 Common string operations}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{standard}{string}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Common string operations.}
6
Barry Warsaw08b07de2004-08-25 03:09:58 +00007The \module{string} module contains a number of useful constants and classes,
Barry Warsaw8bee7612004-08-25 02:22:30 +00008as well as some deprecated legacy functions that are also available as methods
9on strings. See the module \refmodule{re}\refstmodindex{re} for string
10functions based on regular expressions.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011
Barry Warsaw8bee7612004-08-25 02:22:30 +000012\subsection{String constants}
Guido van Rossum0bf4d891995-03-02 12:37:30 +000013
Andrew M. Kuchlingbe063022000-12-26 16:14:32 +000014The constants defined in this module are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015
Fred Drake960fdf92001-07-20 18:38:26 +000016\begin{datadesc}{ascii_letters}
17 The concatenation of the \constant{ascii_lowercase} and
18 \constant{ascii_uppercase} constants described below. This value is
19 not locale-dependent.
20\end{datadesc}
21
22\begin{datadesc}{ascii_lowercase}
23 The lowercase letters \code{'abcdefghijklmnopqrstuvwxyz'}. This
24 value is not locale-dependent and will not change.
25\end{datadesc}
26
27\begin{datadesc}{ascii_uppercase}
28 The uppercase letters \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. This
29 value is not locale-dependent and will not change.
30\end{datadesc}
31
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032\begin{datadesc}{digits}
33 The string \code{'0123456789'}.
34\end{datadesc}
35
36\begin{datadesc}{hexdigits}
37 The string \code{'0123456789abcdefABCDEF'}.
38\end{datadesc}
39
40\begin{datadesc}{letters}
Fred Drake0682be42000-04-10 18:35:49 +000041 The concatenation of the strings \constant{lowercase} and
Fred Drake960fdf92001-07-20 18:38:26 +000042 \constant{uppercase} described below. The specific value is
43 locale-dependent, and will be updated when
44 \function{locale.setlocale()} is called.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000045\end{datadesc}
46
47\begin{datadesc}{lowercase}
48 A string containing all the characters that are considered lowercase
49 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000050 \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000051 the effect on the routines \function{upper()} and
Fred Drake960fdf92001-07-20 18:38:26 +000052 \function{swapcase()} is undefined. The specific value is
53 locale-dependent, and will be updated when
54 \function{locale.setlocale()} is called.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055\end{datadesc}
56
57\begin{datadesc}{octdigits}
58 The string \code{'01234567'}.
59\end{datadesc}
60
Fred Drake480abc22000-09-18 16:48:13 +000061\begin{datadesc}{punctuation}
62 String of \ASCII{} characters which are considered punctuation
63 characters in the \samp{C} locale.
64\end{datadesc}
65
66\begin{datadesc}{printable}
67 String of characters which are considered printable. This is a
68 combination of \constant{digits}, \constant{letters},
69 \constant{punctuation}, and \constant{whitespace}.
70\end{datadesc}
71
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072\begin{datadesc}{uppercase}
73 A string containing all the characters that are considered uppercase
74 letters. On most systems this is the string
Guido van Rossum86751151995-02-28 17:14:32 +000075 \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000076 the effect on the routines \function{lower()} and
Fred Drake960fdf92001-07-20 18:38:26 +000077 \function{swapcase()} is undefined. The specific value is
78 locale-dependent, and will be updated when
79 \function{locale.setlocale()} is called.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{datadesc}
81
82\begin{datadesc}{whitespace}
83 A string containing all characters that are considered whitespace.
84 On most systems this includes the characters space, tab, linefeed,
Guido van Rossum86751151995-02-28 17:14:32 +000085 return, formfeed, and vertical tab. Do not change its definition ---
Fred Drakecce10901998-03-17 06:33:25 +000086 the effect on the routines \function{strip()} and \function{split()}
87 is undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088\end{datadesc}
89
Barry Warsaw8bee7612004-08-25 02:22:30 +000090\subsection{Template strings}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091
Barry Warsaw33db6562004-09-18 21:13:43 +000092Templates provide simpler string substitutions as described in \pep{292}.
93Instead of the normal \samp{\%}-based substitutions, Templates support
Barry Warsaw8bee7612004-08-25 02:22:30 +000094\samp{\$}-based substitutions, using the following rules:
95
96\begin{itemize}
97\item \samp{\$\$} is an escape; it is replaced with a single \samp{\$}.
98
99\item \samp{\$identifier} names a substitution placeholder matching a mapping
100 key of "identifier". By default, "identifier" must spell a Python
101 identifier. The first non-identifier character after the \samp{\$}
102 character terminates this placeholder specification.
103
104\item \samp{\$\{identifier\}} is equivalent to \samp{\$identifier}. It is
105 required when valid identifier characters follow the placeholder but are
Raymond Hettinger785c65c2004-09-06 01:01:08 +0000106 not part of the placeholder, such as "\$\{noun\}ification".
Barry Warsaw8bee7612004-08-25 02:22:30 +0000107\end{itemize}
108
109Any other appearance of \samp{\$} in the string will result in a
110\exception{ValueError} being raised.
111
Raymond Hettinger785c65c2004-09-06 01:01:08 +0000112\versionadded{2.4}
113
Barry Warsaw33db6562004-09-18 21:13:43 +0000114The \module{string} module provides a \class{Template} class that implements
115these rules. The methods of \class{Template} are:
116
117\begin{classdesc}{Template}{template}
118The constructor takes a single argument which is the template string.
119\end{classdesc}
120
121\begin{methoddesc}[Template]{substitute}{mapping\optional{, **kws}}
122Performs the template substitution, returning a new string. \var{mapping} is
123any dictionary-like object with keys that match the placeholders in the
124template. Alternatively, you can provide keyword arguments, where the
125keywords are the placeholders. When both \var{mapping} and \var{kws} are
126given and there are duplicates, the placeholders from \var{kws} take
127precedence.
128\end{methoddesc}
129
130\begin{methoddesc}[Template]{safe_substitute}{mapping\optional{, **kws}}
131Like \method{substitute()}, except that if placeholders are missing from
132\var{mapping} and \var{kws}, instead of raising a \exception{KeyError}
133exception, the original placeholder will appear in the resulting string
134intact. Note that other exceptions may still be raised, including
135\exception{ValueError} as described above.
136\end{methoddesc}
137
138\class{Template} instances also provide one public data attribute:
139
140\begin{memberdesc}[string]{template}
141This is the object passed to the constructor's \var{template} argument. In
142general, you shouldn't change it, but read-only access is not enforced.
143\end{memberdesc}
144
145Here is an example of how to use a Template:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000146
147\begin{verbatim}
148>>> from string import Template
149>>> s = Template('$who likes $what')
Barry Warsaw33db6562004-09-18 21:13:43 +0000150>>> s.substitute(who='tim', what='kung pao')
151'tim likes kung pao'
152>>> d = dict(who='tim')
153>>> Template('Give $who $100').substitute(d)
Barry Warsaw8bee7612004-08-25 02:22:30 +0000154Traceback (most recent call last):
155[...]
Barry Warsaw33db6562004-09-18 21:13:43 +0000156ValueError: Invalid placeholder in string: line 1, col 10
157>>> Template('$who likes $what').substitute(d)
158Traceback (most recent call last):
159[...]
160KeyError: 'what'
161>>> Template('$who likes $what').safe_substitute(d)
162'tim likes $what'
Barry Warsaw8bee7612004-08-25 02:22:30 +0000163\end{verbatim}
164
Barry Warsaw33db6562004-09-18 21:13:43 +0000165Advanced usage: you can derive subclasses of \class{Template} to customize the
166placeholder syntax, delimiter character, or the entire regular expression used
167to parse template strings. To do this, you can override these class
168attributes:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000169
Barry Warsaw33db6562004-09-18 21:13:43 +0000170\begin{itemize}
171\item \var{delimiter} -- This is the literal string describing a placeholder
172 introducing delimiter. The default value \samp{\$}. Note that this
173 should \emph{not} be a regular expression, as the implementation will
174 call \method{re.escape()} on this string as needed.
175\item \var{idpattern} -- This is the regular expression describing the pattern
176 for non-braced placeholders (the braces will be added automatically as
177 appropriate). The default value is the regular expression
178 \samp{[_a-z][_a-z0-9]*}.
179\end{itemize}
Barry Warsaw8bee7612004-08-25 02:22:30 +0000180
Barry Warsaw33db6562004-09-18 21:13:43 +0000181Alternatively, you can provide the entire regular expression pattern by
182overriding the class attribute \var{pattern}. If you do this, the value must
183be a regular expression object with four named capturing groups. The
Barry Warsaw8bee7612004-08-25 02:22:30 +0000184capturing groups correspond to the rules given above, along with the invalid
185placeholder rule:
186
187\begin{itemize}
Barry Warsaw33db6562004-09-18 21:13:43 +0000188\item \var{escaped} -- This group matches the escape sequence,
189 e.g. \samp{\$\$}, in the default pattern.
Barry Warsaw8bee7612004-08-25 02:22:30 +0000190\item \var{named} -- This group matches the unbraced placeholder name; it
Barry Warsaw33db6562004-09-18 21:13:43 +0000191 should not include the delimiter in capturing group.
192\item \var{braced} -- This group matches the brace enclosed placeholder name;
193 it should not include either the delimiter or braces in the capturing
Barry Warsaw8bee7612004-08-25 02:22:30 +0000194 group.
Barry Warsaw33db6562004-09-18 21:13:43 +0000195\item \var{invalid} -- This group matches any other delimiter pattern (usually
196 a single delimiter), and it should appear last in the regular
197 expression.
Barry Warsaw8bee7612004-08-25 02:22:30 +0000198\end{itemize}
199
200\subsection{String functions}
201
202The following functions are available to operate on string and Unicode
203objects. They are not available as string methods.
204
205\begin{funcdesc}{capwords}{s}
206 Split the argument into words using \function{split()}, capitalize
207 each word using \function{capitalize()}, and join the capitalized
208 words using \function{join()}. Note that this replaces runs of
209 whitespace characters by a single space, and removes leading and
210 trailing whitespace.
211\end{funcdesc}
212
213\begin{funcdesc}{maketrans}{from, to}
214 Return a translation table suitable for passing to
215 \function{translate()} or \function{regex.compile()}, that will map
216 each character in \var{from} into the character at the same position
217 in \var{to}; \var{from} and \var{to} must have the same length.
218
219 \warning{Don't use strings derived from \constant{lowercase}
220 and \constant{uppercase} as arguments; in some locales, these don't have
221 the same length. For case conversions, always use
222 \function{lower()} and \function{upper()}.}
223\end{funcdesc}
224
225\subsection{Deprecated string functions}
226
227The following list of functions are also defined as methods of string and
228Unicode objects; see ``String Methods'' (section
229\ref{string-methods}) for more information on those. You should consider
230these functions as deprecated, although they will not be removed until Python
2313.0. The functions defined in this module are:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000232
233\begin{funcdesc}{atof}{s}
Fred Drake15f06662000-10-04 13:59:52 +0000234 \deprecated{2.0}{Use the \function{float()} built-in function.}
Fred Drakee8489761998-12-21 18:56:13 +0000235 Convert a string to a floating point number. The string must have
236 the standard syntax for a floating point literal in Python,
Fred Drake70a66c91999-02-18 16:08:36 +0000237 optionally preceded by a sign (\samp{+} or \samp{-}). Note that
238 this behaves identical to the built-in function
239 \function{float()}\bifuncindex{float} when passed a string.
240
Fred Drake0aa811c2001-10-20 04:24:09 +0000241 \note{When passing in a string, values for NaN\index{NaN}
Fred Drake70a66c91999-02-18 16:08:36 +0000242 and Infinity\index{Infinity} may be returned, depending on the
243 underlying C library. The specific set of strings accepted which
244 cause these values to be returned depends entirely on the C library
Fred Drake0aa811c2001-10-20 04:24:09 +0000245 and is known to vary.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000246\end{funcdesc}
247
Fred Drakecce10901998-03-17 06:33:25 +0000248\begin{funcdesc}{atoi}{s\optional{, base}}
Fred Drake15f06662000-10-04 13:59:52 +0000249 \deprecated{2.0}{Use the \function{int()} built-in function.}
Fred Drakee8489761998-12-21 18:56:13 +0000250 Convert string \var{s} to an integer in the given \var{base}. The
251 string must consist of one or more digits, optionally preceded by a
252 sign (\samp{+} or \samp{-}). The \var{base} defaults to 10. If it
253 is 0, a default base is chosen depending on the leading characters
254 of the string (after stripping the sign): \samp{0x} or \samp{0X}
255 means 16, \samp{0} means 8, anything else means 10. If \var{base}
Fred Drakefffe5db2000-09-21 05:25:30 +0000256 is 16, a leading \samp{0x} or \samp{0X} is always accepted, though
257 not required. This behaves identically to the built-in function
258 \function{int()} when passed a string. (Also note: for a more
259 flexible interpretation of numeric literals, use the built-in
260 function \function{eval()}\bifuncindex{eval}.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000261\end{funcdesc}
262
Fred Drakecce10901998-03-17 06:33:25 +0000263\begin{funcdesc}{atol}{s\optional{, base}}
Fred Drake15f06662000-10-04 13:59:52 +0000264 \deprecated{2.0}{Use the \function{long()} built-in function.}
Fred Drakee8489761998-12-21 18:56:13 +0000265 Convert string \var{s} to a long integer in the given \var{base}.
266 The string must consist of one or more digits, optionally preceded
267 by a sign (\samp{+} or \samp{-}). The \var{base} argument has the
268 same meaning as for \function{atoi()}. A trailing \samp{l} or
269 \samp{L} is not allowed, except if the base is 0. Note that when
270 invoked without \var{base} or with \var{base} set to 10, this
271 behaves identical to the built-in function
272 \function{long()}\bifuncindex{long} when passed a string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000273\end{funcdesc}
274
Guido van Rossume5e55d71996-08-09 21:44:51 +0000275\begin{funcdesc}{capitalize}{word}
Fred Drake473f46a2002-06-20 21:18:46 +0000276 Return a copy of \var{word} with only its first character capitalized.
Guido van Rossume5e55d71996-08-09 21:44:51 +0000277\end{funcdesc}
278
Fred Drake15f06662000-10-04 13:59:52 +0000279\begin{funcdesc}{expandtabs}{s\optional{, tabsize}}
Raymond Hettinger785c65c2004-09-06 01:01:08 +0000280 Expand tabs in a string replacing them by one or more spaces,
Fred Drakee8489761998-12-21 18:56:13 +0000281 depending on the current column and the given tab size. The column
282 number is reset to zero after each newline occurring in the string.
283 This doesn't understand other non-printing characters or escape
Guido van Rossum9700e9b1999-01-25 22:31:53 +0000284 sequences. The tab size defaults to 8.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000285\end{funcdesc}
286
Fred Drakecce10901998-03-17 06:33:25 +0000287\begin{funcdesc}{find}{s, sub\optional{, start\optional{,end}}}
Fred Drakee8489761998-12-21 18:56:13 +0000288 Return the lowest index in \var{s} where the substring \var{sub} is
289 found such that \var{sub} is wholly contained in
290 \code{\var{s}[\var{start}:\var{end}]}. Return \code{-1} on failure.
291 Defaults for \var{start} and \var{end} and interpretation of
292 negative values is the same as for slices.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000293\end{funcdesc}
294
Fred Drakecce10901998-03-17 06:33:25 +0000295\begin{funcdesc}{rfind}{s, sub\optional{, start\optional{, end}}}
Fred Drakee8489761998-12-21 18:56:13 +0000296 Like \function{find()} but find the highest index.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000297\end{funcdesc}
298
Fred Drakecce10901998-03-17 06:33:25 +0000299\begin{funcdesc}{index}{s, sub\optional{, start\optional{, end}}}
Fred Drakee8489761998-12-21 18:56:13 +0000300 Like \function{find()} but raise \exception{ValueError} when the
301 substring is not found.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000302\end{funcdesc}
303
Fred Drakecce10901998-03-17 06:33:25 +0000304\begin{funcdesc}{rindex}{s, sub\optional{, start\optional{, end}}}
Fred Drakee8489761998-12-21 18:56:13 +0000305 Like \function{rfind()} but raise \exception{ValueError} when the
306 substring is not found.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000307\end{funcdesc}
308
Fred Drakecce10901998-03-17 06:33:25 +0000309\begin{funcdesc}{count}{s, sub\optional{, start\optional{, end}}}
Fred Drakee8489761998-12-21 18:56:13 +0000310 Return the number of (non-overlapping) occurrences of substring
311 \var{sub} in string \code{\var{s}[\var{start}:\var{end}]}.
312 Defaults for \var{start} and \var{end} and interpretation of
Andrew M. Kuchlinga4ca07c2000-06-21 01:48:46 +0000313 negative values are the same as for slices.
Guido van Rossumab3a2501994-08-01 12:18:36 +0000314\end{funcdesc}
315
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000316\begin{funcdesc}{lower}{s}
Fred Drakee8489761998-12-21 18:56:13 +0000317 Return a copy of \var{s}, but with upper case letters converted to
318 lower case.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000319\end{funcdesc}
320
Fred Drakecce10901998-03-17 06:33:25 +0000321\begin{funcdesc}{split}{s\optional{, sep\optional{, maxsplit}}}
Fred Drakee8489761998-12-21 18:56:13 +0000322 Return a list of the words of the string \var{s}. If the optional
323 second argument \var{sep} is absent or \code{None}, the words are
324 separated by arbitrary strings of whitespace characters (space, tab,
325 newline, return, formfeed). If the second argument \var{sep} is
326 present and not \code{None}, it specifies a string to be used as the
Fred Drakea7ce52b01999-05-27 17:18:08 +0000327 word separator. The returned list will then have one more item
Fred Drakee8489761998-12-21 18:56:13 +0000328 than the number of non-overlapping occurrences of the separator in
329 the string. The optional third argument \var{maxsplit} defaults to
330 0. If it is nonzero, at most \var{maxsplit} number of splits occur,
331 and the remainder of the string is returned as the final element of
332 the list (thus, the list will have at most \code{\var{maxsplit}+1}
333 elements).
Nicholas Bastin07973da2004-03-21 16:59:59 +0000334
335 The behavior of split on an empty string depends on the value of \var{sep}.
336 If \var{sep} is not specified, or specified as \code{None}, the result will
337 be an empty list. If \var{sep} is specified as any string, the result will
338 be a list containing one element which is an empty string.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000339\end{funcdesc}
340
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000341\begin{funcdesc}{rsplit}{s\optional{, sep\optional{, maxsplit}}}
Hye-Shik Changc6f066f2003-12-17 02:49:03 +0000342 Return a list of the words of the string \var{s}, scanning \var{s}
343 from the end. To all intents and purposes, the resulting list of
344 words is the same as returned by \function{split()}, except when the
345 optional third argument \var{maxsplit} is explicitly specified and
346 nonzero. When \var{maxsplit} is nonzero, at most \var{maxsplit}
Fred Drake32fef9f2003-12-30 23:08:14 +0000347 number of splits -- the \emph{rightmost} ones -- occur, and the remainder
Hye-Shik Changc6f066f2003-12-17 02:49:03 +0000348 of the string is returned as the first element of the list (thus, the
349 list will have at most \code{\var{maxsplit}+1} elements).
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000350 \versionadded{2.4}
351\end{funcdesc}
352
Fred Drakecce10901998-03-17 06:33:25 +0000353\begin{funcdesc}{splitfields}{s\optional{, sep\optional{, maxsplit}}}
Fred Drakee8489761998-12-21 18:56:13 +0000354 This function behaves identically to \function{split()}. (In the
355 past, \function{split()} was only used with one argument, while
356 \function{splitfields()} was only used with two arguments.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000357\end{funcdesc}
358
Fred Drakecce10901998-03-17 06:33:25 +0000359\begin{funcdesc}{join}{words\optional{, sep}}
Fred Drakee8489761998-12-21 18:56:13 +0000360 Concatenate a list or tuple of words with intervening occurrences of
361 \var{sep}. The default value for \var{sep} is a single space
362 character. It is always true that
363 \samp{string.join(string.split(\var{s}, \var{sep}), \var{sep})}
364 equals \var{s}.
Guido van Rossume5e55d71996-08-09 21:44:51 +0000365\end{funcdesc}
366
Fred Drakecce10901998-03-17 06:33:25 +0000367\begin{funcdesc}{joinfields}{words\optional{, sep}}
Fred Drakeb7c18952002-09-12 14:16:07 +0000368 This function behaves identically to \function{join()}. (In the past,
Fred Drakee8489761998-12-21 18:56:13 +0000369 \function{join()} was only used with one argument, while
370 \function{joinfields()} was only used with two arguments.)
Fred Drakeb7c18952002-09-12 14:16:07 +0000371 Note that there is no \method{joinfields()} method on string
372 objects; use the \method{join()} method instead.
Guido van Rossume5e55d71996-08-09 21:44:51 +0000373\end{funcdesc}
374
Walter Dörwaldde02bcb2002-04-22 17:42:37 +0000375\begin{funcdesc}{lstrip}{s\optional{, chars}}
376Return a copy of the string with leading characters removed. If
377\var{chars} is omitted or \code{None}, whitespace characters are
378removed. If given and not \code{None}, \var{chars} must be a string;
379the characters in the string will be stripped from the beginning of
380the string this method is called on.
Neal Norwitzffe33b72003-04-10 22:35:32 +0000381\versionchanged[The \var{chars} parameter was added. The \var{chars}
382parameter cannot be passed in earlier 2.2 versions]{2.2.3}
Guido van Rossume5e55d71996-08-09 21:44:51 +0000383\end{funcdesc}
384
Walter Dörwaldde02bcb2002-04-22 17:42:37 +0000385\begin{funcdesc}{rstrip}{s\optional{, chars}}
386Return a copy of the string with trailing characters removed. If
387\var{chars} is omitted or \code{None}, whitespace characters are
388removed. If given and not \code{None}, \var{chars} must be a string;
389the characters in the string will be stripped from the end of the
390string this method is called on.
Neal Norwitzffe33b72003-04-10 22:35:32 +0000391\versionchanged[The \var{chars} parameter was added. The \var{chars}
Martin v. Löwisb0c319a2004-07-19 16:34:01 +0000392parameter cannot be passed in earlier 2.2 versions]{2.2.3}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000393\end{funcdesc}
394
Walter Dörwaldde02bcb2002-04-22 17:42:37 +0000395\begin{funcdesc}{strip}{s\optional{, chars}}
396Return a copy of the string with leading and trailing characters
397removed. If \var{chars} is omitted or \code{None}, whitespace
398characters are removed. If given and not \code{None}, \var{chars}
399must be a string; the characters in the string will be stripped from
400the both ends of the string this method is called on.
Neal Norwitzffe33b72003-04-10 22:35:32 +0000401\versionchanged[The \var{chars} parameter was added. The \var{chars}
Neal Norwitza6bdf2a2003-04-17 23:07:13 +0000402parameter cannot be passed in earlier 2.2 versions]{2.2.3}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000403\end{funcdesc}
404
405\begin{funcdesc}{swapcase}{s}
Fred Drakee8489761998-12-21 18:56:13 +0000406 Return a copy of \var{s}, but with lower case letters
407 converted to upper case and vice versa.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000408\end{funcdesc}
409
Guido van Rossumf4d0d571996-07-30 18:23:05 +0000410\begin{funcdesc}{translate}{s, table\optional{, deletechars}}
Fred Drakee8489761998-12-21 18:56:13 +0000411 Delete all characters from \var{s} that are in \var{deletechars} (if
412 present), and then translate the characters using \var{table}, which
413 must be a 256-character string giving the translation for each
Raymond Hettinger5c5fca92003-07-13 02:06:47 +0000414 character value, indexed by its ordinal.
Guido van Rossumf65f2781995-09-13 17:37:21 +0000415\end{funcdesc}
416
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000417\begin{funcdesc}{upper}{s}
Fred Drakee8489761998-12-21 18:56:13 +0000418 Return a copy of \var{s}, but with lower case letters converted to
419 upper case.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000420\end{funcdesc}
421
Fred Drakecce10901998-03-17 06:33:25 +0000422\begin{funcdesc}{ljust}{s, width}
423\funcline{rjust}{s, width}
424\funcline{center}{s, width}
Fred Drakee8489761998-12-21 18:56:13 +0000425 These functions respectively left-justify, right-justify and center
426 a string in a field of given width. They return a string that is at
427 least \var{width} characters wide, created by padding the string
428 \var{s} with spaces until the given width on the right, left or both
429 sides. The string is never truncated.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000430\end{funcdesc}
431
Fred Drakecce10901998-03-17 06:33:25 +0000432\begin{funcdesc}{zfill}{s, width}
Fred Drakee8489761998-12-21 18:56:13 +0000433 Pad a numeric string on the left with zero digits until the given
434 width is reached. Strings starting with a sign are handled
435 correctly.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000436\end{funcdesc}
Guido van Rossum0bf4d891995-03-02 12:37:30 +0000437
Martin v. Löwis8bafb2a2003-11-18 19:48:57 +0000438\begin{funcdesc}{replace}{str, old, new\optional{, maxreplace}}
Fred Drakee8489761998-12-21 18:56:13 +0000439 Return a copy of string \var{str} with all occurrences of substring
440 \var{old} replaced by \var{new}. If the optional argument
Martin v. Löwis8bafb2a2003-11-18 19:48:57 +0000441 \var{maxreplace} is given, the first \var{maxreplace} occurrences are
Fred Drakee8489761998-12-21 18:56:13 +0000442 replaced.
Guido van Rossumc8a80cd1997-03-25 16:41:31 +0000443\end{funcdesc}