Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 1 | # module 'string' -- A collection of string operations |
| 2 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3 | # Warning: most of the code you see here isn't normally used nowadays. With |
| 4 | # Python 1.6, many of these functions are implemented as methods on the |
| 5 | # standard string object. They used to be implemented by a built-in module |
| 6 | # called strop, but strop is now obsolete itself. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 8 | """Common string manipulations. |
| 9 | |
| 10 | Public module variables: |
| 11 | |
| 12 | whitespace -- a string containing all characters considered whitespace |
| 13 | lowercase -- a string containing all characters considered lowercase letters |
| 14 | uppercase -- a string containing all characters considered uppercase letters |
| 15 | letters -- a string containing all characters considered letters |
| 16 | digits -- a string containing all characters considered decimal digits |
| 17 | hexdigits -- a string containing all characters considered hexadecimal digits |
| 18 | octdigits -- a string containing all characters considered octal digits |
| 19 | |
| 20 | """ |
| 21 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 22 | # Some strings for ctype-style character classification |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 23 | whitespace = ' \t\n\r\v\f' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 24 | lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 25 | uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 26 | letters = lowercase + uppercase |
| 27 | digits = '0123456789' |
| 28 | hexdigits = digits + 'abcdef' + 'ABCDEF' |
| 29 | octdigits = '01234567' |
| 30 | |
| 31 | # Case conversion helpers |
Guido van Rossum | a61ff7b | 1992-01-14 18:31:29 +0000 | [diff] [blame] | 32 | _idmap = '' |
| 33 | for i in range(256): _idmap = _idmap + chr(i) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 34 | del i |
| 35 | |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 36 | # Backward compatible names for exceptions |
| 37 | index_error = ValueError |
| 38 | atoi_error = ValueError |
| 39 | atof_error = ValueError |
| 40 | atol_error = ValueError |
| 41 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 42 | # convert UPPER CASE letters to lower case |
| 43 | def lower(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 44 | """lower(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 45 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 46 | Return a copy of the string s converted to lowercase. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 47 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 48 | """ |
| 49 | return s.lower() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 50 | |
| 51 | # Convert lower case letters to UPPER CASE |
| 52 | def upper(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 53 | """upper(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 54 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 55 | Return a copy of the string s converted to uppercase. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 56 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 57 | """ |
| 58 | return s.upper() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 59 | |
| 60 | # Swap lower case letters and UPPER CASE |
| 61 | def swapcase(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 62 | """swapcase(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 63 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 64 | Return a copy of the string s with upper case characters |
| 65 | converted to lowercase and vice versa. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 66 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 67 | """ |
| 68 | return s.swapcase() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 69 | |
| 70 | # Strip leading and trailing tabs and spaces |
| 71 | def strip(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 72 | """strip(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 73 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 74 | Return a copy of the string s with leading and trailing |
| 75 | whitespace removed. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 76 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 77 | """ |
| 78 | return s.strip() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 80 | # Strip leading tabs and spaces |
| 81 | def lstrip(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 82 | """lstrip(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 83 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 84 | Return a copy of the string s with leading whitespace removed. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 85 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 86 | """ |
| 87 | return s.lstrip() |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 88 | |
| 89 | # Strip trailing tabs and spaces |
| 90 | def rstrip(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 91 | """rstrip(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 92 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 93 | Return a copy of the string s with trailing whitespace |
| 94 | removed. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 95 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 96 | """ |
| 97 | return s.rstrip() |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 98 | |
| 99 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 100 | # Split a string into a list of space/tab-separated words |
| 101 | # NB: split(s) is NOT the same as splitfields(s, ' ')! |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 102 | def split(s, sep=None, maxsplit=0): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 103 | """split(str [,sep [,maxsplit]]) -> list of strings |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 104 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 105 | Return a list of the words in the string s, using sep as the |
| 106 | delimiter string. If maxsplit is nonzero, splits into at most |
| 107 | maxsplit words If sep is not specified, any whitespace string |
| 108 | is a separator. Maxsplit defaults to 0. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 109 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 110 | (split and splitfields are synonymous) |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 111 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 112 | """ |
| 113 | return s.split(sep, maxsplit) |
| 114 | splitfields = split |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 2ab1992 | 1995-06-22 18:58:00 +0000 | [diff] [blame] | 116 | # Join fields with optional separator |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 117 | def join(words, sep = ' '): |
| 118 | """join(list [,sep]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 119 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 120 | Return a string composed of the words in list, with |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 121 | intervening occurrences of sep. The default separator is a |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 122 | single space. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 123 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 124 | (joinfields and join are synonymous) |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 125 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 126 | """ |
| 127 | return sep.join(words) |
| 128 | joinfields = join |
| 129 | |
| 130 | # for a little bit of speed |
| 131 | _apply = apply |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 133 | # Find substring, raise exception if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 134 | def index(s, *args): |
| 135 | """index(s, sub [,start [,end]]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 136 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 137 | Like find but raises ValueError when the substring is not found. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 138 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 139 | """ |
| 140 | return _apply(s.index, args) |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 142 | # Find last substring, raise exception if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 143 | def rindex(s, *args): |
| 144 | """rindex(s, sub [,start [,end]]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 145 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 146 | Like rfind but raises ValueError when the substring is not found. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 147 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 148 | """ |
| 149 | return _apply(s.rindex, args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 150 | |
| 151 | # Count non-overlapping occurrences of substring |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 152 | def count(s, *args): |
| 153 | """count(s, sub[, start[,end]]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 154 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 155 | Return the number of occurrences of substring sub in string |
| 156 | s[start:end]. Optional arguments start and end are |
| 157 | interpreted as in slice notation. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 158 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 159 | """ |
| 160 | return _apply(s.count, args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 162 | # Find substring, return -1 if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 163 | def find(s, *args): |
| 164 | """find(s, sub [,start [,end]]) -> in |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 165 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 166 | Return the lowest index in s where substring sub is found, |
| 167 | such that sub is contained within s[start,end]. Optional |
| 168 | arguments start and end are interpreted as in slice notation. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 169 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 170 | Return -1 on failure. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 171 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 172 | """ |
| 173 | return _apply(s.find, args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 175 | # Find last substring, return -1 if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 176 | def rfind(s, *args): |
| 177 | """rfind(s, sub [,start [,end]]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 178 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 179 | Return the highest index in s where substring sub is found, |
| 180 | such that sub is contained within s[start,end]. Optional |
| 181 | arguments start and end are interpreted as in slice notation. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 182 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 183 | Return -1 on failure. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 184 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 185 | """ |
| 186 | return _apply(s.rfind, args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 187 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 188 | # for a bit of speed |
| 189 | _float = float |
| 190 | _int = int |
| 191 | _long = long |
| 192 | _StringType = type('') |
Guido van Rossum | d0753e2 | 1997-12-10 22:59:55 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 194 | # Convert string to float |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 195 | def atof(s): |
| 196 | """atof(s) -> float |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 197 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 198 | Return the floating point number represented by the string s. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 199 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 200 | """ |
| 201 | if type(s) == _StringType: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 202 | return _float(s) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 203 | else: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 204 | raise TypeError('argument 1: expected string, %s found' % |
| 205 | type(s).__name__) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 207 | # Convert string to integer |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 208 | def atoi(*args): |
| 209 | """atoi(s [,base]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 210 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 211 | Return the integer represented by the string s in the given |
| 212 | base, which defaults to 10. The string s must consist of one |
| 213 | or more digits, possibly preceded by a sign. If base is 0, it |
| 214 | is chosen from the leading characters of s, 0 for octal, 0x or |
| 215 | 0X for hexadecimal. If base is 16, a preceding 0x or 0X is |
| 216 | accepted. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 217 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 218 | """ |
| 219 | try: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 220 | s = args[0] |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 221 | except IndexError: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 222 | raise TypeError('function requires at least 1 argument: %d given' % |
| 223 | len(args)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 224 | # Don't catch type error resulting from too many arguments to int(). The |
| 225 | # error message isn't compatible but the error type is, and this function |
| 226 | # is complicated enough already. |
| 227 | if type(s) == _StringType: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 228 | return _apply(_int, args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 229 | else: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 230 | raise TypeError('argument 1: expected string, %s found' % |
| 231 | type(s).__name__) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 234 | # Convert string to long integer |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 235 | def atol(*args): |
| 236 | """atol(s [,base]) -> long |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 237 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 238 | Return the long integer represented by the string s in the |
| 239 | given base, which defaults to 10. The string s must consist |
| 240 | of one or more digits, possibly preceded by a sign. If base |
| 241 | is 0, it is chosen from the leading characters of s, 0 for |
| 242 | octal, 0x or 0X for hexadecimal. If base is 16, a preceding |
| 243 | 0x or 0X is accepted. A trailing L or l is not accepted, |
| 244 | unless base is 0. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 245 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 246 | """ |
| 247 | try: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 248 | s = args[0] |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 249 | except IndexError: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 250 | raise TypeError('function requires at least 1 argument: %d given' % |
| 251 | len(args)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 252 | # Don't catch type error resulting from too many arguments to long(). The |
| 253 | # error message isn't compatible but the error type is, and this function |
| 254 | # is complicated enough already. |
| 255 | if type(s) == _StringType: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 256 | return _apply(_long, args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 257 | else: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 258 | raise TypeError('argument 1: expected string, %s found' % |
| 259 | type(s).__name__) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 262 | # Left-justify a string |
| 263 | def ljust(s, width): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 264 | """ljust(s, width) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 265 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 266 | Return a left-justified version of s, in a field of the |
| 267 | specified width, padded with spaces as needed. The string is |
| 268 | never truncated. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 269 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 270 | """ |
| 271 | n = width - len(s) |
| 272 | if n <= 0: return s |
| 273 | return s + ' '*n |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 274 | |
| 275 | # Right-justify a string |
| 276 | def rjust(s, width): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 277 | """rjust(s, width) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 278 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 279 | Return a right-justified version of s, in a field of the |
| 280 | specified width, padded with spaces as needed. The string is |
| 281 | never truncated. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 282 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 283 | """ |
| 284 | n = width - len(s) |
| 285 | if n <= 0: return s |
| 286 | return ' '*n + s |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 287 | |
| 288 | # Center a string |
| 289 | def center(s, width): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 290 | """center(s, width) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 291 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 292 | Return a center version of s, in a field of the specified |
| 293 | width. padded with spaces as needed. The string is never |
| 294 | truncated. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 295 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 296 | """ |
| 297 | n = width - len(s) |
| 298 | if n <= 0: return s |
| 299 | half = n/2 |
| 300 | if n%2 and width%2: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 301 | # This ensures that center(center(s, i), j) = center(s, j) |
| 302 | half = half+1 |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 303 | return ' '*half + s + ' '*(n-half) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 304 | |
| 305 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
| 306 | # Decadent feature: the argument may be a string or a number |
| 307 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
| 308 | def zfill(x, width): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 309 | """zfill(x, width) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 310 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 311 | Pad a numeric string x with zeros on the left, to fill a field |
| 312 | of the specified width. The string x is never truncated. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 313 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 314 | """ |
| 315 | if type(x) == type(''): s = x |
| 316 | else: s = `x` |
| 317 | n = len(s) |
| 318 | if n >= width: return s |
| 319 | sign = '' |
| 320 | if s[0] in ('-', '+'): |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 321 | sign, s = s[0], s[1:] |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 322 | return sign + '0'*(width-n) + s |
Guido van Rossum | 6ff2e90 | 1992-03-27 15:13:31 +0000 | [diff] [blame] | 323 | |
| 324 | # Expand tabs in a string. |
| 325 | # Doesn't take non-printing chars into account, but does understand \n. |
Guido van Rossum | 894a7bb | 1995-08-10 19:42:05 +0000 | [diff] [blame] | 326 | def expandtabs(s, tabsize=8): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 327 | """expandtabs(s [,tabsize]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 328 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 329 | Return a copy of the string s with all tab characters replaced |
| 330 | by the appropriate number of spaces, depending on the current |
| 331 | column, and the tabsize (default 8). |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 332 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 333 | """ |
| 334 | res = line = '' |
| 335 | for c in s: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 336 | if c == '\t': |
| 337 | c = ' '*(tabsize - len(line) % tabsize) |
| 338 | line = line + c |
| 339 | if c == '\n': |
| 340 | res = res + line |
| 341 | line = '' |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 342 | return res + line |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 343 | |
Guido van Rossum | 2539528 | 1996-05-28 23:08:45 +0000 | [diff] [blame] | 344 | # Character translation through look-up table. |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 345 | def translate(s, table, deletions=""): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 346 | """translate(s,table [,deletechars]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 347 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 348 | Return a copy of the string s, where all characters occurring |
| 349 | in the optional argument deletechars are removed, and the |
| 350 | remaining characters have been mapped through the given |
| 351 | translation table, which must be a string of length 256. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 352 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 353 | """ |
| 354 | return s.translate(table, deletions) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 356 | # Capitalize a string, e.g. "aBc dEf" -> "Abc def". |
| 357 | def capitalize(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 358 | """capitalize(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 359 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 360 | Return a copy of the string s with only its first character |
| 361 | capitalized. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 362 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 363 | """ |
| 364 | return s.capitalize() |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 365 | |
| 366 | # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
| 367 | # See also regsub.capwords(). |
Guido van Rossum | 34f1731 | 1996-08-20 20:25:41 +0000 | [diff] [blame] | 368 | def capwords(s, sep=None): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 369 | """capwords(s, [sep]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 370 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 371 | Split the argument into words using split, capitalize each |
| 372 | word using capitalize, and join the capitalized words using |
| 373 | join. Note that this replaces runs of whitespace characters by |
| 374 | a single space. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 375 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 376 | """ |
| 377 | return join(map(capitalize, s.split(sep)), sep or ' ') |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 378 | |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 379 | # Construct a translation string |
| 380 | _idmapL = None |
| 381 | def maketrans(fromstr, tostr): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 382 | """maketrans(frm, to) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 383 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 384 | Return a translation table (a string of 256 bytes long) |
| 385 | suitable for use in string.translate. The strings frm and to |
| 386 | must be of the same length. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 387 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 388 | """ |
| 389 | if len(fromstr) != len(tostr): |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 390 | raise ValueError, "maketrans arguments must have same length" |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 391 | global _idmapL |
| 392 | if not _idmapL: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 393 | _idmapL = map(None, _idmap) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 394 | L = _idmapL[:] |
| 395 | fromstr = map(ord, fromstr) |
| 396 | for i in range(len(fromstr)): |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 397 | L[fromstr[i]] = tostr[i] |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 398 | return joinfields(L, "") |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | 1eb9a81 | 1997-03-25 16:50:31 +0000 | [diff] [blame] | 400 | # Substring replacement (global) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 401 | def replace(s, old, new, maxsplit=0): |
| 402 | """replace (str, old, new[, maxsplit]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 403 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 404 | Return a copy of string str with all occurrences of substring |
| 405 | old replaced by new. If the optional argument maxsplit is |
| 406 | given, only the first maxsplit occurrences are replaced. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 407 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 408 | """ |
| 409 | return s.replace(old, new, maxsplit) |
Guido van Rossum | 1eb9a81 | 1997-03-25 16:50:31 +0000 | [diff] [blame] | 410 | |
| 411 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 412 | # XXX: transitional |
| 413 | # |
| 414 | # If string objects do not have methods, then we need to use the old string.py |
| 415 | # library, which uses strop for many more things than just the few outlined |
| 416 | # below. |
| 417 | try: |
| 418 | ''.upper |
| 419 | except AttributeError: |
| 420 | from stringold import * |
| 421 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 422 | # Try importing optional built-in module "strop" -- if it exists, |
| 423 | # it redefines some string operations that are 100-1000 times faster. |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 424 | # It also defines values for whitespace, lowercase and uppercase |
| 425 | # that match <ctype.h>'s definitions. |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 426 | |
| 427 | try: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 428 | from strop import maketrans, lowercase, uppercase, whitespace |
| 429 | letters = lowercase + uppercase |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 430 | except ImportError: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 431 | pass # Use the original versions |