Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{string}} |
| 2 | |
| 3 | \stmodindex{string} |
| 4 | |
| 5 | This module defines some constants useful for checking character |
| 6 | classes, some exceptions, and some useful string functions. |
| 7 | The constants are: |
| 8 | |
| 9 | \renewcommand{\indexsubitem}{(data in module string)} |
| 10 | \begin{datadesc}{digits} |
| 11 | The string \code{'0123456789'}. |
| 12 | \end{datadesc} |
| 13 | |
| 14 | \begin{datadesc}{hexdigits} |
| 15 | The string \code{'0123456789abcdefABCDEF'}. |
| 16 | \end{datadesc} |
| 17 | |
| 18 | \begin{datadesc}{letters} |
| 19 | The concatenation of the strings \code{lowercase} and |
| 20 | \code{uppercase} described below. |
| 21 | \end{datadesc} |
| 22 | |
| 23 | \begin{datadesc}{lowercase} |
| 24 | A string containing all the characters that are considered lowercase |
| 25 | letters. On most systems this is the string |
| 26 | \code{'abcdefghijklmnopqrstuvwxyz'}. Do not change its definition -- |
| 27 | the effect on the routines \code{upper} and \code{swapcase} is |
| 28 | undefined. |
| 29 | \end{datadesc} |
| 30 | |
| 31 | \begin{datadesc}{octdigits} |
| 32 | The string \code{'01234567'}. |
| 33 | \end{datadesc} |
| 34 | |
| 35 | \begin{datadesc}{uppercase} |
| 36 | A string containing all the characters that are considered uppercase |
| 37 | letters. On most systems this is the string |
| 38 | \code{'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. Do not change its definition -- |
| 39 | the effect on the routines \code{lower} and \code{swapcase} is |
| 40 | undefined. |
| 41 | \end{datadesc} |
| 42 | |
| 43 | \begin{datadesc}{whitespace} |
| 44 | A string containing all characters that are considered whitespace. |
| 45 | On most systems this includes the characters space, tab, linefeed, |
| 46 | return, formfeed, and vertical tab. Do not change its definition -- |
| 47 | the effect on the routines \code{strip} and \code{split} is |
| 48 | undefined. |
| 49 | \end{datadesc} |
| 50 | |
| 51 | The exceptions are: |
| 52 | |
| 53 | \renewcommand{\indexsubitem}{(exception in module string)} |
| 54 | |
| 55 | \begin{excdesc}{atof_error} |
| 56 | Exception raised by |
| 57 | \code{atof} |
| 58 | when a non-float string argument is detected. |
| 59 | The exception argument is the offending string. |
| 60 | \end{excdesc} |
| 61 | |
| 62 | \begin{excdesc}{atoi_error} |
| 63 | Exception raised by |
| 64 | \code{atoi} |
| 65 | when a non-integer string argument is detected. |
| 66 | The exception argument is the offending string. |
| 67 | \end{excdesc} |
| 68 | |
| 69 | \begin{excdesc}{atol_error} |
| 70 | Exception raised by |
| 71 | \code{atol} |
| 72 | when a non-integer string argument is detected. |
| 73 | The exception argument is the offending string. |
| 74 | \end{excdesc} |
| 75 | |
| 76 | \begin{excdesc}{index_error} |
| 77 | Exception raised by \code{index} when \var{sub} is not found. |
| 78 | The exception argument is undefined (it may be a tuple containing the |
| 79 | offending arguments to \code{index} or it may be the constant string |
| 80 | \code{'substring not found'}). |
| 81 | \end{excdesc} |
| 82 | |
| 83 | The functions are: |
| 84 | |
| 85 | \renewcommand{\indexsubitem}{(in module string)} |
| 86 | |
| 87 | \begin{funcdesc}{atof}{s} |
| 88 | Convert a string to a floating point number. The string must have |
| 89 | the standard syntax for a floating point literal in Python, optionally |
| 90 | preceded by a sign (\samp{+} or \samp{-}). |
| 91 | \end{funcdesc} |
| 92 | |
| 93 | \begin{funcdesc}{atoi}{s} |
| 94 | Convert a string to an integer. The string must consist of one or more |
| 95 | digits, optionally preceded by a sign (\samp{+} or \samp{-}). |
| 96 | \end{funcdesc} |
| 97 | |
| 98 | \begin{funcdesc}{atol}{s} |
| 99 | Convert a string to a long integer. The string must consist of one |
| 100 | or more digits, optionally preceded by a sign (\samp{+} or \samp{-}). |
| 101 | \end{funcdesc} |
| 102 | |
| 103 | \begin{funcdesc}{expandtabs}{s\, tabsize} |
| 104 | Expand tabs in a string, i.e. replace them by one or more spaces, |
| 105 | depending on the current column and the given tab size. The column |
| 106 | number is reset to zero after each newline occurring in the string. |
| 107 | This doesn't understand other non-printing characters or escape |
| 108 | sequences. |
| 109 | \end{funcdesc} |
| 110 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 111 | \begin{funcdesc}{find}{s\, sub\optional{\, start}} |
| 112 | Return the lowest index in \var{s} not smaller than \var{start} where the |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 113 | substring \var{sub} is found. Return \code{-1} when \var{sub} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 114 | does not occur as a substring of \var{s} with index at least \var{start}. |
| 115 | If \var{start} is omitted, it defaults to \code{0}. If \var{start} is |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 116 | negative, \code{len(\var{s})} is added. |
| 117 | \end{funcdesc} |
| 118 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 119 | \begin{funcdesc}{rfind}{s\, sub\optional{\, start}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 120 | Like \code{find} but finds the highest index. |
| 121 | \end{funcdesc} |
| 122 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 123 | \begin{funcdesc}{index}{s\, sub\optional{\, start}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 124 | Like \code{find} but raise \code{index_error} when the substring is |
| 125 | not found. |
| 126 | \end{funcdesc} |
| 127 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 128 | \begin{funcdesc}{rindex}{s\, sub\optional{\, start}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 129 | Like \code{rfind} but raise \code{index_error} when the substring is |
| 130 | not found. |
| 131 | \end{funcdesc} |
| 132 | |
Guido van Rossum | ab3a250 | 1994-08-01 12:18:36 +0000 | [diff] [blame] | 133 | \begin{funcdesc}{count}{s\, sub\, i} |
| 134 | Return the number of (non-overlapping) occurrences of substring |
| 135 | \var{sub} in string \var{s} with index at least \var{i}. |
| 136 | If \var{i} is omitted, it defaults to \code{0}. |
| 137 | \end{funcdesc} |
| 138 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 139 | \begin{funcdesc}{lower}{s} |
| 140 | Convert letters to lower case. |
| 141 | \end{funcdesc} |
| 142 | |
| 143 | \begin{funcdesc}{split}{s} |
| 144 | Returns a list of the whitespace-delimited words of the string |
| 145 | \var{s}. |
| 146 | \end{funcdesc} |
| 147 | |
| 148 | \begin{funcdesc}{splitfields}{s\, sep} |
| 149 | Returns a list containing the fields of the string \var{s}, using |
| 150 | the string \var{sep} as a separator. The list will have one more |
| 151 | items than the number of non-overlapping occurrences of the |
| 152 | separator in the string. Thus, \code{string.splitfields(\var{s}, ' |
| 153 | ')} is not the same as \code{string.split(\var{s})}, as the latter |
| 154 | only returns non-empty words. As a special case, |
| 155 | \code{splitfields(\var{s}, '')} returns \code{[\var{s}]}, for any string |
| 156 | \var{s}. (See also \code{regsub.split()}.) |
| 157 | \end{funcdesc} |
| 158 | |
| 159 | \begin{funcdesc}{join}{words} |
| 160 | Concatenate a list or tuple of words with intervening spaces. |
| 161 | \end{funcdesc} |
| 162 | |
| 163 | \begin{funcdesc}{joinfields}{words\, sep} |
| 164 | Concatenate a list or tuple of words with intervening separators. |
| 165 | It is always true that |
| 166 | \code{string.joinfields(string.splitfields(\var{t}, \var{sep}), \var{sep})} |
| 167 | equals \var{t}. |
| 168 | \end{funcdesc} |
| 169 | |
| 170 | \begin{funcdesc}{strip}{s} |
| 171 | Removes leading and trailing whitespace from the string |
| 172 | \var{s}. |
| 173 | \end{funcdesc} |
| 174 | |
| 175 | \begin{funcdesc}{swapcase}{s} |
| 176 | Converts lower case letters to upper case and vice versa. |
| 177 | \end{funcdesc} |
| 178 | |
| 179 | \begin{funcdesc}{upper}{s} |
| 180 | Convert letters to upper case. |
| 181 | \end{funcdesc} |
| 182 | |
| 183 | \begin{funcdesc}{ljust}{s\, width} |
| 184 | \funcline{rjust}{s\, width} |
| 185 | \funcline{center}{s\, width} |
| 186 | These functions respectively left-justify, right-justify and center a |
| 187 | string in a field of given width. |
| 188 | They return a string that is at least |
| 189 | \var{width} |
| 190 | characters wide, created by padding the string |
| 191 | \var{s} |
| 192 | with spaces until the given width on the right, left or both sides. |
| 193 | The string is never truncated. |
| 194 | \end{funcdesc} |
| 195 | |
| 196 | \begin{funcdesc}{zfill}{s\, width} |
| 197 | Pad a numeric string on the left with zero digits until the given |
| 198 | width is reached. Strings starting with a sign are handled correctly. |
| 199 | \end{funcdesc} |