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