| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \section{\module{string} --- | 
					
						
							| 
									
										
										
										
											1999-04-22 21:23:22 +00:00
										 |  |  |          Common string operations} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 21:23:22 +00:00
										 |  |  | \declaremodule{standard}{string} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | \modulesynopsis{Common string operations.} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 02:22:30 +00:00
										 |  |  | The \module{string} package contains a number of useful constants and classes, | 
					
						
							|  |  |  | as well as some deprecated legacy functions that are also available as methods | 
					
						
							|  |  |  | on strings.  See the module \refmodule{re}\refstmodindex{re} for string | 
					
						
							|  |  |  | functions based on regular expressions. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 02:22:30 +00:00
										 |  |  | In general, all of these objects are exposed directly in the \module{string} | 
					
						
							|  |  |  | package so users need only import the \module{string} package to begin using | 
					
						
							|  |  |  | these constants, classes, and functions. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{notice} | 
					
						
							|  |  |  | Starting with Python 2.4, the traditional \module{string} module was turned | 
					
						
							|  |  |  | into a package, however backward compatibility with existing code has been | 
					
						
							|  |  |  | retained.  Code using the \module{string} module that worked prior to Python | 
					
						
							|  |  |  | 2.4 should continue to work unchanged. | 
					
						
							|  |  |  | \end{notice} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \subsection{String constants} | 
					
						
							| 
									
										
										
										
											1995-03-02 12:37:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-26 16:14:32 +00:00
										 |  |  | The constants defined in this module are: | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-20 18:38:26 +00:00
										 |  |  | \begin{datadesc}{ascii_letters} | 
					
						
							|  |  |  |   The concatenation of the \constant{ascii_lowercase} and | 
					
						
							|  |  |  |   \constant{ascii_uppercase} constants described below.  This value is | 
					
						
							|  |  |  |   not locale-dependent. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{ascii_lowercase} | 
					
						
							|  |  |  |   The lowercase letters \code{'abcdefghijklmnopqrstuvwxyz'}.  This | 
					
						
							|  |  |  |   value is not locale-dependent and will not change. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{ascii_uppercase} | 
					
						
							|  |  |  |   The uppercase letters \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}.  This | 
					
						
							|  |  |  |   value is not locale-dependent and will not change. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{datadesc}{digits} | 
					
						
							|  |  |  |   The string \code{'0123456789'}. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{hexdigits} | 
					
						
							|  |  |  |   The string \code{'0123456789abcdefABCDEF'}. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{letters} | 
					
						
							| 
									
										
										
										
											2000-04-10 18:35:49 +00:00
										 |  |  |   The concatenation of the strings \constant{lowercase} and | 
					
						
							| 
									
										
										
										
											2001-07-20 18:38:26 +00:00
										 |  |  |   \constant{uppercase} described below.  The specific value is | 
					
						
							|  |  |  |   locale-dependent, and will be updated when | 
					
						
							|  |  |  |   \function{locale.setlocale()} is called. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{lowercase} | 
					
						
							|  |  |  |   A string containing all the characters that are considered lowercase | 
					
						
							|  |  |  |   letters.  On most systems this is the string | 
					
						
							| 
									
										
										
										
											1995-02-28 17:14:32 +00:00
										 |  |  |   \code{'abcdefghijklmnopqrstuvwxyz'}.  Do not change its definition --- | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  |   the effect on the routines \function{upper()} and | 
					
						
							| 
									
										
										
										
											2001-07-20 18:38:26 +00:00
										 |  |  |   \function{swapcase()} is undefined.  The specific value is | 
					
						
							|  |  |  |   locale-dependent, and will be updated when | 
					
						
							|  |  |  |   \function{locale.setlocale()} is called. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{octdigits} | 
					
						
							|  |  |  |   The string \code{'01234567'}. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-18 16:48:13 +00:00
										 |  |  | \begin{datadesc}{punctuation} | 
					
						
							|  |  |  |   String of \ASCII{} characters which are considered punctuation | 
					
						
							|  |  |  |   characters in the \samp{C} locale. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{printable} | 
					
						
							|  |  |  |   String of characters which are considered printable.  This is a | 
					
						
							|  |  |  |   combination of \constant{digits}, \constant{letters}, | 
					
						
							|  |  |  |   \constant{punctuation}, and \constant{whitespace}. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{datadesc}{uppercase} | 
					
						
							|  |  |  |   A string containing all the characters that are considered uppercase | 
					
						
							|  |  |  |   letters.  On most systems this is the string | 
					
						
							| 
									
										
										
										
											1995-02-28 17:14:32 +00:00
										 |  |  |   \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}.  Do not change its definition --- | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  |   the effect on the routines \function{lower()} and | 
					
						
							| 
									
										
										
										
											2001-07-20 18:38:26 +00:00
										 |  |  |   \function{swapcase()} is undefined.  The specific value is | 
					
						
							|  |  |  |   locale-dependent, and will be updated when | 
					
						
							|  |  |  |   \function{locale.setlocale()} is called. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{whitespace} | 
					
						
							|  |  |  |   A string containing all characters that are considered whitespace. | 
					
						
							|  |  |  |   On most systems this includes the characters space, tab, linefeed, | 
					
						
							| 
									
										
										
										
											1995-02-28 17:14:32 +00:00
										 |  |  |   return, formfeed, and vertical tab.  Do not change its definition --- | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  |   the effect on the routines \function{strip()} and \function{split()} | 
					
						
							|  |  |  |   is undefined. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 02:22:30 +00:00
										 |  |  | \subsection{Template strings} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Templates are Unicode strings that can be used to provide string substitutions | 
					
						
							|  |  |  | as described in \pep{292}.  There is a \class{Template} class that is a | 
					
						
							|  |  |  | subclass of \class{unicode}, overriding the default \method{__mod__()} method. | 
					
						
							|  |  |  | Instead of the normal \samp{\%}-based substitutions, Template strings support | 
					
						
							|  |  |  | \samp{\$}-based substitutions, using the following rules: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{itemize} | 
					
						
							|  |  |  | \item \samp{\$\$} is an escape; it is replaced with a single \samp{\$}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item \samp{\$identifier} names a substitution placeholder matching a mapping | 
					
						
							|  |  |  |        key of "identifier".  By default, "identifier" must spell a Python | 
					
						
							|  |  |  |        identifier.  The first non-identifier character after the \samp{\$} | 
					
						
							|  |  |  |        character terminates this placeholder specification. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item \samp{\$\{identifier\}} is equivalent to \samp{\$identifier}.  It is | 
					
						
							|  |  |  |       required when valid identifier characters follow the placeholder but are | 
					
						
							|  |  |  |       not part of the placeholder, e.g. "\$\{noun\}ification". | 
					
						
							|  |  |  | \end{itemize} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Any other appearance of \samp{\$} in the string will result in a | 
					
						
							|  |  |  | \exception{ValueError} being raised. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Template strings are used just like normal strings, in that the modulus | 
					
						
							|  |  |  | operator is used to interpolate a dictionary of values into a Template string, | 
					
						
							|  |  |  | e.g.: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | >>> from string import Template | 
					
						
							|  |  |  | >>> s = Template('$who likes $what') | 
					
						
							|  |  |  | >>> print s % dict(who='tim', what='kung pao')
 | 
					
						
							|  |  |  | tim likes kung pao | 
					
						
							|  |  |  | >>> Template('Give $who $100') % dict(who='tim')
 | 
					
						
							|  |  |  | Traceback (most recent call last): | 
					
						
							|  |  |  | [...] | 
					
						
							|  |  |  | ValueError: Invalid placeholder at index 10 | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | There is also a \class{SafeTemplate} class, derived from \class{Template} | 
					
						
							|  |  |  | which acts the same as \class{Template}, except that if placeholders are | 
					
						
							|  |  |  | missing in the interpolation dictionary, no \exception{KeyError} will be | 
					
						
							|  |  |  | raised.  Instead the original placeholder (with or without the braces, as | 
					
						
							|  |  |  | appropriate) will be used: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | >>> from string import SafeTemplate | 
					
						
							|  |  |  | >>> s = SafeTemplate('$who likes $what for ${meal}')
 | 
					
						
							|  |  |  | >>> print s % dict(who='tim')
 | 
					
						
							|  |  |  | tim likes $what for ${meal} | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The values in the mapping will automatically be converted to Unicode strings, | 
					
						
							|  |  |  | using the built-in \function{unicode()} function, which will be called without | 
					
						
							|  |  |  | optional arguments \var{encoding} or \var{errors}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Advanced usage: you can derive subclasses of \class{Template} or | 
					
						
							|  |  |  | \class{SafeTemplate} to use application-specific placeholder rules.  To do | 
					
						
							|  |  |  | this, you override the class attribute \member{pattern}; the value must be a | 
					
						
							|  |  |  | compiled regular expression object with four named capturing groups.  The | 
					
						
							|  |  |  | capturing groups correspond to the rules given above, along with the invalid | 
					
						
							|  |  |  | placeholder rule: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{itemize} | 
					
						
							|  |  |  | \item \var{escaped} -- This group matches the escape sequence, i.e. \samp{\$\$} | 
					
						
							|  |  |  |       in the default pattern. | 
					
						
							|  |  |  | \item \var{named} -- This group matches the unbraced placeholder name; it | 
					
						
							|  |  |  |       should not include the \samp{\$} in capturing group. | 
					
						
							|  |  |  | \item \var{braced} -- This group matches the brace delimited placeholder name; | 
					
						
							|  |  |  |       it should not include either the \samp{\$} or braces in the capturing | 
					
						
							|  |  |  |       group. | 
					
						
							|  |  |  | \item \var{bogus} -- This group matches any other \samp{\$}.  It usually just | 
					
						
							|  |  |  |       matches a single \samp{\$} and should appear last. | 
					
						
							|  |  |  | \end{itemize} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \subsection{String functions} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The following functions are available to operate on string and Unicode | 
					
						
							|  |  |  | objects.  They are not available as string methods. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{capwords}{s} | 
					
						
							|  |  |  |   Split the argument into words using \function{split()}, capitalize | 
					
						
							|  |  |  |   each word using \function{capitalize()}, and join the capitalized | 
					
						
							|  |  |  |   words using \function{join()}.  Note that this replaces runs of | 
					
						
							|  |  |  |   whitespace characters by a single space, and removes leading and | 
					
						
							|  |  |  |   trailing whitespace. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{maketrans}{from, to} | 
					
						
							|  |  |  |   Return a translation table suitable for passing to | 
					
						
							|  |  |  |   \function{translate()} or \function{regex.compile()}, that will map | 
					
						
							|  |  |  |   each character in \var{from} into the character at the same position | 
					
						
							|  |  |  |   in \var{to}; \var{from} and \var{to} must have the same length. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   \warning{Don't use strings derived from \constant{lowercase} | 
					
						
							|  |  |  |   and \constant{uppercase} as arguments; in some locales, these don't have | 
					
						
							|  |  |  |   the same length.  For case conversions, always use | 
					
						
							|  |  |  |   \function{lower()} and \function{upper()}.} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 02:22:30 +00:00
										 |  |  | \subsection{Deprecated string functions} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The following list of functions are also defined as methods of string and | 
					
						
							|  |  |  | Unicode objects; see ``String Methods'' (section | 
					
						
							|  |  |  | \ref{string-methods}) for more information on those.  You should consider | 
					
						
							|  |  |  | these functions as deprecated, although they will not be removed until Python | 
					
						
							|  |  |  | 3.0.  The functions defined in this module are: | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{atof}{s} | 
					
						
							| 
									
										
										
										
											2000-10-04 13:59:52 +00:00
										 |  |  |   \deprecated{2.0}{Use the \function{float()} built-in function.} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Convert a string to a floating point number.  The string must have | 
					
						
							|  |  |  |   the standard syntax for a floating point literal in Python, | 
					
						
							| 
									
										
										
										
											1999-02-18 16:08:36 +00:00
										 |  |  |   optionally preceded by a sign (\samp{+} or \samp{-}).  Note that | 
					
						
							|  |  |  |   this behaves identical to the built-in function | 
					
						
							|  |  |  |   \function{float()}\bifuncindex{float} when passed a string. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  |   \note{When passing in a string, values for NaN\index{NaN} | 
					
						
							| 
									
										
										
										
											1999-02-18 16:08:36 +00:00
										 |  |  |   and Infinity\index{Infinity} may be returned, depending on the | 
					
						
							|  |  |  |   underlying C library.  The specific set of strings accepted which | 
					
						
							|  |  |  |   cause these values to be returned depends entirely on the C library | 
					
						
							| 
									
										
										
										
											2001-10-20 04:24:09 +00:00
										 |  |  |   and is known to vary.} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{atoi}{s\optional{, base}} | 
					
						
							| 
									
										
										
										
											2000-10-04 13:59:52 +00:00
										 |  |  |   \deprecated{2.0}{Use the \function{int()} built-in function.} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Convert string \var{s} to an integer in the given \var{base}.  The | 
					
						
							|  |  |  |   string must consist of one or more digits, optionally preceded by a | 
					
						
							|  |  |  |   sign (\samp{+} or \samp{-}).  The \var{base} defaults to 10.  If it | 
					
						
							|  |  |  |   is 0, a default base is chosen depending on the leading characters | 
					
						
							|  |  |  |   of the string (after stripping the sign): \samp{0x} or \samp{0X} | 
					
						
							|  |  |  |   means 16, \samp{0} means 8, anything else means 10.  If \var{base} | 
					
						
							| 
									
										
										
										
											2000-09-21 05:25:30 +00:00
										 |  |  |   is 16, a leading \samp{0x} or \samp{0X} is always accepted, though | 
					
						
							|  |  |  |   not required.  This behaves identically to the built-in function | 
					
						
							|  |  |  |   \function{int()} when passed a string.  (Also note: for a more | 
					
						
							|  |  |  |   flexible interpretation of numeric literals, use the built-in | 
					
						
							|  |  |  |   function \function{eval()}\bifuncindex{eval}.) | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{atol}{s\optional{, base}} | 
					
						
							| 
									
										
										
										
											2000-10-04 13:59:52 +00:00
										 |  |  |   \deprecated{2.0}{Use the \function{long()} built-in function.} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Convert string \var{s} to a long integer in the given \var{base}. | 
					
						
							|  |  |  |   The string must consist of one or more digits, optionally preceded | 
					
						
							|  |  |  |   by a sign (\samp{+} or \samp{-}).  The \var{base} argument has the | 
					
						
							|  |  |  |   same meaning as for \function{atoi()}.  A trailing \samp{l} or | 
					
						
							|  |  |  |   \samp{L} is not allowed, except if the base is 0.  Note that when | 
					
						
							|  |  |  |   invoked without \var{base} or with \var{base} set to 10, this | 
					
						
							|  |  |  |   behaves identical to the built-in function | 
					
						
							|  |  |  |   \function{long()}\bifuncindex{long} when passed a string. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-09 21:44:51 +00:00
										 |  |  | \begin{funcdesc}{capitalize}{word} | 
					
						
							| 
									
										
										
										
											2002-06-20 21:18:46 +00:00
										 |  |  |   Return a copy of \var{word} with only its first character capitalized. | 
					
						
							| 
									
										
										
										
											1996-08-09 21:44:51 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-04 13:59:52 +00:00
										 |  |  | \begin{funcdesc}{expandtabs}{s\optional{, tabsize}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Expand tabs in a string, i.e.\ replace them by one or more spaces, | 
					
						
							|  |  |  |   depending on the current column and the given tab size.  The column | 
					
						
							|  |  |  |   number is reset to zero after each newline occurring in the string. | 
					
						
							|  |  |  |   This doesn't understand other non-printing characters or escape | 
					
						
							| 
									
										
										
										
											1999-01-25 22:31:53 +00:00
										 |  |  |   sequences.  The tab size defaults to 8. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{find}{s, sub\optional{, start\optional{,end}}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Return the lowest index in \var{s} where the substring \var{sub} is | 
					
						
							|  |  |  |   found such that \var{sub} is wholly contained in | 
					
						
							|  |  |  |   \code{\var{s}[\var{start}:\var{end}]}.  Return \code{-1} on failure. | 
					
						
							|  |  |  |   Defaults for \var{start} and \var{end} and interpretation of | 
					
						
							|  |  |  |   negative values is the same as for slices. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{rfind}{s, sub\optional{, start\optional{, end}}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Like \function{find()} but find the highest index. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{index}{s, sub\optional{, start\optional{, end}}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Like \function{find()} but raise \exception{ValueError} when the | 
					
						
							|  |  |  |   substring is not found. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{rindex}{s, sub\optional{, start\optional{, end}}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Like \function{rfind()} but raise \exception{ValueError} when the | 
					
						
							|  |  |  |   substring is not found. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{count}{s, sub\optional{, start\optional{, end}}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Return the number of (non-overlapping) occurrences of substring | 
					
						
							|  |  |  |   \var{sub} in string \code{\var{s}[\var{start}:\var{end}]}. | 
					
						
							|  |  |  |   Defaults for \var{start} and \var{end} and interpretation of | 
					
						
							| 
									
										
										
										
											2000-06-21 01:48:46 +00:00
										 |  |  |   negative values are the same as for slices. | 
					
						
							| 
									
										
										
										
											1994-08-01 12:18:36 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{lower}{s} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Return a copy of \var{s}, but with upper case letters converted to | 
					
						
							|  |  |  |   lower case. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{split}{s\optional{, sep\optional{, maxsplit}}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Return a list of the words of the string \var{s}.  If the optional | 
					
						
							|  |  |  |   second argument \var{sep} is absent or \code{None}, the words are | 
					
						
							|  |  |  |   separated by arbitrary strings of whitespace characters (space, tab,  | 
					
						
							|  |  |  |   newline, return, formfeed).  If the second argument \var{sep} is | 
					
						
							|  |  |  |   present and not \code{None}, it specifies a string to be used as the  | 
					
						
							| 
									
										
										
										
											1999-05-27 17:18:08 +00:00
										 |  |  |   word separator.  The returned list will then have one more item | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   than the number of non-overlapping occurrences of the separator in | 
					
						
							|  |  |  |   the string.  The optional third argument \var{maxsplit} defaults to | 
					
						
							|  |  |  |   0.  If it is nonzero, at most \var{maxsplit} number of splits occur, | 
					
						
							|  |  |  |   and the remainder of the string is returned as the final element of | 
					
						
							|  |  |  |   the list (thus, the list will have at most \code{\var{maxsplit}+1} | 
					
						
							|  |  |  |   elements). | 
					
						
							| 
									
										
										
										
											2004-03-21 16:59:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   The behavior of split on an empty string depends on the value of \var{sep}. | 
					
						
							|  |  |  |   If \var{sep} is not specified, or specified as \code{None}, the result will | 
					
						
							|  |  |  |   be an empty list.  If \var{sep} is specified as any string, the result will | 
					
						
							|  |  |  |   be a list containing one element which is an empty string. | 
					
						
							| 
									
										
										
										
											1996-08-09 21:44:51 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-15 18:49:53 +00:00
										 |  |  | \begin{funcdesc}{rsplit}{s\optional{, sep\optional{, maxsplit}}} | 
					
						
							| 
									
										
										
										
											2003-12-17 02:49:03 +00:00
										 |  |  |   Return a list of the words of the string \var{s}, scanning \var{s} | 
					
						
							|  |  |  |   from the end.  To all intents and purposes, the resulting list of | 
					
						
							|  |  |  |   words is the same as returned by \function{split()}, except when the | 
					
						
							|  |  |  |   optional third argument \var{maxsplit} is explicitly specified and | 
					
						
							|  |  |  |   nonzero.  When \var{maxsplit} is nonzero, at most \var{maxsplit} | 
					
						
							| 
									
										
										
										
											2003-12-30 23:08:14 +00:00
										 |  |  |   number of splits -- the \emph{rightmost} ones -- occur, and the remainder | 
					
						
							| 
									
										
										
										
											2003-12-17 02:49:03 +00:00
										 |  |  |   of the string is returned as the first element of the list (thus, the | 
					
						
							|  |  |  |   list will have at most \code{\var{maxsplit}+1} elements). | 
					
						
							| 
									
										
										
										
											2003-12-15 18:49:53 +00:00
										 |  |  |   \versionadded{2.4} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{splitfields}{s\optional{, sep\optional{, maxsplit}}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   This function behaves identically to \function{split()}.  (In the | 
					
						
							|  |  |  |   past, \function{split()} was only used with one argument, while | 
					
						
							|  |  |  |   \function{splitfields()} was only used with two arguments.) | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{join}{words\optional{, sep}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Concatenate a list or tuple of words with intervening occurrences of  | 
					
						
							|  |  |  |   \var{sep}.  The default value for \var{sep} is a single space | 
					
						
							|  |  |  |   character.  It is always true that | 
					
						
							|  |  |  |   \samp{string.join(string.split(\var{s}, \var{sep}), \var{sep})} | 
					
						
							|  |  |  |   equals \var{s}. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{joinfields}{words\optional{, sep}} | 
					
						
							| 
									
										
										
										
											2002-09-12 14:16:07 +00:00
										 |  |  |   This function behaves identically to \function{join()}.  (In the past,  | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   \function{join()} was only used with one argument, while | 
					
						
							|  |  |  |   \function{joinfields()} was only used with two arguments.) | 
					
						
							| 
									
										
										
										
											2002-09-12 14:16:07 +00:00
										 |  |  |   Note that there is no \method{joinfields()} method on string | 
					
						
							|  |  |  |   objects; use the \method{join()} method instead. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-22 17:42:37 +00:00
										 |  |  | \begin{funcdesc}{lstrip}{s\optional{, chars}} | 
					
						
							|  |  |  | Return a copy of the string with leading characters removed.  If | 
					
						
							|  |  |  | \var{chars} is omitted or \code{None}, whitespace characters are | 
					
						
							|  |  |  | removed.  If given and not \code{None}, \var{chars} must be a string; | 
					
						
							|  |  |  | the characters in the string will be stripped from the beginning of | 
					
						
							|  |  |  | the string this method is called on. | 
					
						
							| 
									
										
										
										
											2003-04-10 22:35:32 +00:00
										 |  |  | \versionchanged[The \var{chars} parameter was added.  The \var{chars} | 
					
						
							|  |  |  | parameter cannot be passed in earlier 2.2 versions]{2.2.3} | 
					
						
							| 
									
										
										
										
											1996-08-09 21:44:51 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-22 17:42:37 +00:00
										 |  |  | \begin{funcdesc}{rstrip}{s\optional{, chars}} | 
					
						
							|  |  |  | Return a copy of the string with trailing characters removed.  If | 
					
						
							|  |  |  | \var{chars} is omitted or \code{None}, whitespace characters are | 
					
						
							|  |  |  | removed.  If given and not \code{None}, \var{chars} must be a string; | 
					
						
							|  |  |  | the characters in the string will be stripped from the end of the | 
					
						
							|  |  |  | string this method is called on. | 
					
						
							| 
									
										
										
										
											2003-04-10 22:35:32 +00:00
										 |  |  | \versionchanged[The \var{chars} parameter was added.  The \var{chars} | 
					
						
							| 
									
										
										
										
											2004-07-19 16:34:01 +00:00
										 |  |  | parameter cannot be passed in earlier 2.2 versions]{2.2.3} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-22 17:42:37 +00:00
										 |  |  | \begin{funcdesc}{strip}{s\optional{, chars}} | 
					
						
							|  |  |  | Return a copy of the string with leading and trailing characters | 
					
						
							|  |  |  | removed.  If \var{chars} is omitted or \code{None}, whitespace | 
					
						
							|  |  |  | characters are removed.  If given and not \code{None}, \var{chars} | 
					
						
							|  |  |  | must be a string; the characters in the string will be stripped from | 
					
						
							|  |  |  | the both ends of the string this method is called on. | 
					
						
							| 
									
										
										
										
											2003-04-10 22:35:32 +00:00
										 |  |  | \versionchanged[The \var{chars} parameter was added.  The \var{chars} | 
					
						
							| 
									
										
										
										
											2003-04-17 23:07:13 +00:00
										 |  |  | parameter cannot be passed in earlier 2.2 versions]{2.2.3} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{swapcase}{s} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Return a copy of \var{s}, but with lower case letters | 
					
						
							|  |  |  |   converted to upper case and vice versa. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-07-30 18:23:05 +00:00
										 |  |  | \begin{funcdesc}{translate}{s, table\optional{, deletechars}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Delete all characters from \var{s} that are in \var{deletechars} (if  | 
					
						
							|  |  |  |   present), and then translate the characters using \var{table}, which  | 
					
						
							|  |  |  |   must be a 256-character string giving the translation for each | 
					
						
							| 
									
										
										
										
											2003-07-13 02:06:47 +00:00
										 |  |  |   character value, indexed by its ordinal. | 
					
						
							| 
									
										
										
										
											1995-09-13 17:37:21 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{upper}{s} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Return a copy of \var{s}, but with lower case letters converted to | 
					
						
							|  |  |  |   upper case. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{ljust}{s, width} | 
					
						
							|  |  |  | \funcline{rjust}{s, width} | 
					
						
							|  |  |  | \funcline{center}{s, width} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   These functions respectively left-justify, right-justify and center | 
					
						
							|  |  |  |   a string in a field of given width.  They return a string that is at | 
					
						
							|  |  |  |   least \var{width} characters wide, created by padding the string | 
					
						
							|  |  |  |   \var{s} with spaces until the given width on the right, left or both | 
					
						
							|  |  |  |   sides.  The string is never truncated. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{zfill}{s, width} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Pad a numeric string on the left with zero digits until the given | 
					
						
							|  |  |  |   width is reached.  Strings starting with a sign are handled | 
					
						
							|  |  |  |   correctly. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							| 
									
										
										
										
											1995-03-02 12:37:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-11-18 19:48:57 +00:00
										 |  |  | \begin{funcdesc}{replace}{str, old, new\optional{, maxreplace}} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   Return a copy of string \var{str} with all occurrences of substring | 
					
						
							|  |  |  |   \var{old} replaced by \var{new}.  If the optional argument | 
					
						
							| 
									
										
										
										
											2003-11-18 19:48:57 +00:00
										 |  |  |   \var{maxreplace} is given, the first \var{maxreplace} occurrences are | 
					
						
							| 
									
										
										
										
											1998-12-21 18:56:13 +00:00
										 |  |  |   replaced. | 
					
						
							| 
									
										
										
										
											1997-03-25 16:41:31 +00:00
										 |  |  | \end{funcdesc} |