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