Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{string}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-string} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 3 | \stmodindex{string} |
| 4 | |
| 5 | This module defines some constants useful for checking character |
Fred Drake | 6d2bdb6 | 1997-12-16 04:04:25 +0000 | [diff] [blame] | 6 | classes and some useful string functions. See the module |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 7 | \module{re}\refstmodindex{re} for string functions based on regular |
| 8 | expressions. |
Guido van Rossum | 0bf4d89 | 1995-03-02 12:37:30 +0000 | [diff] [blame] | 9 | |
| 10 | The constants defined in this module are are: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 12 | \begin{datadesc}{digits} |
| 13 | The string \code{'0123456789'}. |
| 14 | \end{datadesc} |
| 15 | |
| 16 | \begin{datadesc}{hexdigits} |
| 17 | The string \code{'0123456789abcdefABCDEF'}. |
| 18 | \end{datadesc} |
| 19 | |
| 20 | \begin{datadesc}{letters} |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 21 | The concatenation of the strings \function{lowercase()} and |
| 22 | \function{uppercase()} described below. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 23 | \end{datadesc} |
| 24 | |
| 25 | \begin{datadesc}{lowercase} |
| 26 | A string containing all the characters that are considered lowercase |
| 27 | letters. On most systems this is the string |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 28 | \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition --- |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 29 | the effect on the routines \function{upper()} and |
| 30 | \function{swapcase()} is undefined. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 31 | \end{datadesc} |
| 32 | |
| 33 | \begin{datadesc}{octdigits} |
| 34 | The string \code{'01234567'}. |
| 35 | \end{datadesc} |
| 36 | |
| 37 | \begin{datadesc}{uppercase} |
| 38 | A string containing all the characters that are considered uppercase |
| 39 | letters. On most systems this is the string |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 40 | \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition --- |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 41 | the effect on the routines \function{lower()} and |
| 42 | \function{swapcase()} is undefined. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 43 | \end{datadesc} |
| 44 | |
| 45 | \begin{datadesc}{whitespace} |
| 46 | A string containing all characters that are considered whitespace. |
| 47 | On most systems this includes the characters space, tab, linefeed, |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 48 | return, formfeed, and vertical tab. Do not change its definition --- |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 49 | the effect on the routines \function{strip()} and \function{split()} |
| 50 | is undefined. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 51 | \end{datadesc} |
| 52 | |
Guido van Rossum | 0bf4d89 | 1995-03-02 12:37:30 +0000 | [diff] [blame] | 53 | The functions defined in this module are: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 55 | |
| 56 | \begin{funcdesc}{atof}{s} |
| 57 | Convert a string to a floating point number. The string must have |
| 58 | the standard syntax for a floating point literal in Python, optionally |
Guido van Rossum | 740eb82 | 1997-04-02 05:56:16 +0000 | [diff] [blame] | 59 | preceded by a sign (\samp{+} or \samp{-}). Note that this behaves |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 60 | identical to the built-in function |
| 61 | \function{float()}\bifuncindex{float} when passed a string. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 62 | \end{funcdesc} |
| 63 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 64 | \begin{funcdesc}{atoi}{s\optional{, base}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 65 | Convert string \var{s} to an integer in the given \var{base}. The |
| 66 | string must consist of one or more digits, optionally preceded by a |
| 67 | sign (\samp{+} or \samp{-}). The \var{base} defaults to 10. If it is |
| 68 | 0, a default base is chosen depending on the leading characters of the |
| 69 | string (after stripping the sign): \samp{0x} or \samp{0X} means 16, |
| 70 | \samp{0} means 8, anything else means 10. If \var{base} is 16, a |
Guido van Rossum | 740eb82 | 1997-04-02 05:56:16 +0000 | [diff] [blame] | 71 | leading \samp{0x} or \samp{0X} is always accepted. Note that when |
| 72 | invoked without \var{base} or with \var{base} set to 10, this behaves |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 73 | identical to the built-in function \function{int()} when passed a string. |
Guido van Rossum | 740eb82 | 1997-04-02 05:56:16 +0000 | [diff] [blame] | 74 | (Also note: for a more flexible interpretation of numeric literals, |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 75 | use the built-in function \function{eval()}\bifuncindex{eval}.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 76 | \end{funcdesc} |
| 77 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 78 | \begin{funcdesc}{atol}{s\optional{, base}} |
| 79 | Convert string \var{s} to a long integer in the given \var{base}. The |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 80 | string must consist of one or more digits, optionally preceded by a |
| 81 | sign (\samp{+} or \samp{-}). The \var{base} argument has the same |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 82 | meaning as for \function{atoi()}. A trailing \samp{l} or \samp{L} is |
| 83 | not allowed, except if the base is 0. Note that when invoked without |
Guido van Rossum | 740eb82 | 1997-04-02 05:56:16 +0000 | [diff] [blame] | 84 | \var{base} or with \var{base} set to 10, this behaves identical to the |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 85 | built-in function \function{long()}\bifuncindex{long} when passed a |
| 86 | string. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 87 | \end{funcdesc} |
| 88 | |
Guido van Rossum | e5e55d7 | 1996-08-09 21:44:51 +0000 | [diff] [blame] | 89 | \begin{funcdesc}{capitalize}{word} |
| 90 | Capitalize the first character of the argument. |
| 91 | \end{funcdesc} |
| 92 | |
| 93 | \begin{funcdesc}{capwords}{s} |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 94 | Split the argument into words using \function{split()}, capitalize |
| 95 | each word using \function{capitalize()}, and join the capitalized |
| 96 | words using \function{join()}. Note that this replaces runs of |
| 97 | whitespace characters by a single space, and removes leading and |
| 98 | trailing whitespace. |
Guido van Rossum | e5e55d7 | 1996-08-09 21:44:51 +0000 | [diff] [blame] | 99 | \end{funcdesc} |
| 100 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 101 | \begin{funcdesc}{expandtabs}{s, tabsize} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 102 | Expand tabs in a string, i.e.\ replace them by one or more spaces, |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 103 | depending on the current column and the given tab size. The column |
| 104 | number is reset to zero after each newline occurring in the string. |
| 105 | This doesn't understand other non-printing characters or escape |
| 106 | sequences. |
| 107 | \end{funcdesc} |
| 108 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 109 | \begin{funcdesc}{find}{s, sub\optional{, start\optional{,end}}} |
Guido van Rossum | 828a0bd | 1997-10-20 22:40:26 +0000 | [diff] [blame] | 110 | Return the lowest index in \var{s} where the substring \var{sub} is |
| 111 | found such that \var{sub} is wholly contained in |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 112 | \code{\var{s}[\var{start}:\var{end}]}. Return \code{-1} on failure. |
Guido van Rossum | 828a0bd | 1997-10-20 22:40:26 +0000 | [diff] [blame] | 113 | Defaults for \var{start} and \var{end} and interpretation of negative |
| 114 | values is the same as for slices. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 115 | \end{funcdesc} |
| 116 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 117 | \begin{funcdesc}{rfind}{s, sub\optional{, start\optional{, end}}} |
| 118 | Like \function{find()} but find the highest index. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 119 | \end{funcdesc} |
| 120 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 121 | \begin{funcdesc}{index}{s, sub\optional{, start\optional{, end}}} |
| 122 | Like \function{find()} but raise \exception{ValueError} when the |
| 123 | substring is not found. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 124 | \end{funcdesc} |
| 125 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 126 | \begin{funcdesc}{rindex}{s, sub\optional{, start\optional{, end}}} |
| 127 | Like \function{rfind()} but raise \exception{ValueError} when the |
| 128 | substring is not found. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 129 | \end{funcdesc} |
| 130 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 131 | \begin{funcdesc}{count}{s, sub\optional{, start\optional{, end}}} |
Guido van Rossum | ab3a250 | 1994-08-01 12:18:36 +0000 | [diff] [blame] | 132 | Return the number of (non-overlapping) occurrences of substring |
Guido van Rossum | 828a0bd | 1997-10-20 22:40:26 +0000 | [diff] [blame] | 133 | \var{sub} in string \code{\var{s}[\var{start}:\var{end}]}. |
| 134 | Defaults for \var{start} and \var{end} and interpretation of negative |
| 135 | values is the same as for slices. |
Guido van Rossum | ab3a250 | 1994-08-01 12:18:36 +0000 | [diff] [blame] | 136 | \end{funcdesc} |
| 137 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 138 | \begin{funcdesc}{lower}{s} |
| 139 | Convert letters to lower case. |
| 140 | \end{funcdesc} |
| 141 | |
Guido van Rossum | f4d0d57 | 1996-07-30 18:23:05 +0000 | [diff] [blame] | 142 | \begin{funcdesc}{maketrans}{from, to} |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 143 | Return a translation table suitable for passing to |
| 144 | \function{translate()} or \function{regex.compile()}, that will map |
| 145 | each character in \var{from} into the character at the same position |
| 146 | in \var{to}; \var{from} and \var{to} must have the same length. |
Guido van Rossum | f4d0d57 | 1996-07-30 18:23:05 +0000 | [diff] [blame] | 147 | \end{funcdesc} |
| 148 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 149 | \begin{funcdesc}{split}{s\optional{, sep\optional{, maxsplit}}} |
Guido van Rossum | e5e55d7 | 1996-08-09 21:44:51 +0000 | [diff] [blame] | 150 | Return a list of the words of the string \var{s}. If the optional |
| 151 | second argument \var{sep} is absent or \code{None}, the words are |
| 152 | separated by arbitrary strings of whitespace characters (space, tab, |
| 153 | newline, return, formfeed). If the second argument \var{sep} is |
| 154 | present and not \code{None}, it specifies a string to be used as the |
| 155 | word separator. The returned list will then have one more items than |
| 156 | the number of non-overlapping occurrences of the separator in the |
| 157 | string. The optional third argument \var{maxsplit} defaults to 0. If |
| 158 | it is nonzero, at most \var{maxsplit} number of splits occur, and the |
| 159 | remainder of the string is returned as the final element of the list |
| 160 | (thus, the list will have at most \code{\var{maxsplit}+1} elements). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 161 | \end{funcdesc} |
| 162 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 163 | \begin{funcdesc}{splitfields}{s\optional{, sep\optional{, maxsplit}}} |
| 164 | This function behaves identically to \function{split()}. (In the |
| 165 | past, \function{split()} was only used with one argument, while |
| 166 | \function{splitfields()} was only used with two arguments.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 167 | \end{funcdesc} |
| 168 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 169 | \begin{funcdesc}{join}{words\optional{, sep}} |
Guido van Rossum | e5e55d7 | 1996-08-09 21:44:51 +0000 | [diff] [blame] | 170 | Concatenate a list or tuple of words with intervening occurrences of |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 171 | \var{sep}. The default value for \var{sep} is a single space |
| 172 | character. It is always true that |
| 173 | \samp{string.join(string.split(\var{s}, \var{sep}), \var{sep})} |
Guido van Rossum | e5e55d7 | 1996-08-09 21:44:51 +0000 | [diff] [blame] | 174 | equals \var{s}. |
| 175 | \end{funcdesc} |
| 176 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 177 | \begin{funcdesc}{joinfields}{words\optional{, sep}} |
| 178 | This function behaves identical to \function{join()}. (In the past, |
| 179 | \function{join()} was only used with one argument, while |
| 180 | \function{joinfields()} was only used with two arguments.) |
Guido van Rossum | e5e55d7 | 1996-08-09 21:44:51 +0000 | [diff] [blame] | 181 | \end{funcdesc} |
| 182 | |
| 183 | \begin{funcdesc}{lstrip}{s} |
| 184 | Remove leading whitespace from the string \var{s}. |
| 185 | \end{funcdesc} |
| 186 | |
| 187 | \begin{funcdesc}{rstrip}{s} |
| 188 | Remove trailing whitespace from the string \var{s}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 189 | \end{funcdesc} |
| 190 | |
| 191 | \begin{funcdesc}{strip}{s} |
Guido van Rossum | e5e55d7 | 1996-08-09 21:44:51 +0000 | [diff] [blame] | 192 | Remove leading and trailing whitespace from the string \var{s}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 193 | \end{funcdesc} |
| 194 | |
| 195 | \begin{funcdesc}{swapcase}{s} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 196 | Convert lower case letters to upper case and vice versa. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 197 | \end{funcdesc} |
| 198 | |
Guido van Rossum | f4d0d57 | 1996-07-30 18:23:05 +0000 | [diff] [blame] | 199 | \begin{funcdesc}{translate}{s, table\optional{, deletechars}} |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 200 | Delete all characters from \var{s} that are in \var{deletechars} (if |
| 201 | present), and then translate the characters using \var{table}, which |
| 202 | must be a 256-character string giving the translation for each |
| 203 | character value, indexed by its ordinal. |
Guido van Rossum | f65f278 | 1995-09-13 17:37:21 +0000 | [diff] [blame] | 204 | \end{funcdesc} |
| 205 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 206 | \begin{funcdesc}{upper}{s} |
| 207 | Convert letters to upper case. |
| 208 | \end{funcdesc} |
| 209 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 210 | \begin{funcdesc}{ljust}{s, width} |
| 211 | \funcline{rjust}{s, width} |
| 212 | \funcline{center}{s, width} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 213 | These functions respectively left-justify, right-justify and center a |
| 214 | string in a field of given width. |
| 215 | They return a string that is at least |
| 216 | \var{width} |
| 217 | characters wide, created by padding the string |
| 218 | \var{s} |
| 219 | with spaces until the given width on the right, left or both sides. |
| 220 | The string is never truncated. |
| 221 | \end{funcdesc} |
| 222 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 223 | \begin{funcdesc}{zfill}{s, width} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 224 | Pad a numeric string on the left with zero digits until the given |
| 225 | width is reached. Strings starting with a sign are handled correctly. |
| 226 | \end{funcdesc} |
Guido van Rossum | 0bf4d89 | 1995-03-02 12:37:30 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | 740eb82 | 1997-04-02 05:56:16 +0000 | [diff] [blame] | 228 | \begin{funcdesc}{replace}{str, old, new\optional{, maxsplit}} |
Guido van Rossum | c8a80cd | 1997-03-25 16:41:31 +0000 | [diff] [blame] | 229 | Return a copy of string \var{str} with all occurrences of substring |
Guido van Rossum | 740eb82 | 1997-04-02 05:56:16 +0000 | [diff] [blame] | 230 | \var{old} replaced by \var{new}. If the optional argument |
| 231 | \var{maxsplit} is given, the first \var{maxsplit} occurrences are |
| 232 | replaced. |
Guido van Rossum | c8a80cd | 1997-03-25 16:41:31 +0000 | [diff] [blame] | 233 | \end{funcdesc} |
| 234 | |
Guido van Rossum | 0bf4d89 | 1995-03-02 12:37:30 +0000 | [diff] [blame] | 235 | This module is implemented in Python. Much of its functionality has |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 236 | been reimplemented in the built-in module |
| 237 | \module{strop}\refbimodindex{strop}. However, you |
Guido van Rossum | 0bf4d89 | 1995-03-02 12:37:30 +0000 | [diff] [blame] | 238 | should \emph{never} import the latter module directly. When |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 239 | \module{string} discovers that \module{strop} exists, it transparently |
| 240 | replaces parts of itself with the implementation from \module{strop}. |
Guido van Rossum | 0bf4d89 | 1995-03-02 12:37:30 +0000 | [diff] [blame] | 241 | After initialization, there is \emph{no} overhead in using |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 242 | \module{string} instead of \module{strop}. |