Skip Montanaro | 0b87444 | 2003-10-03 14:05:26 +0000 | [diff] [blame] | 1 | """A collection of string operations (most are no longer used). |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 2 | |
Skip Montanaro | 0b87444 | 2003-10-03 14:05:26 +0000 | [diff] [blame] | 3 | Warning: most of the code you see here isn't normally used nowadays. |
| 4 | Beginning with Python 1.6, many of these functions are implemented as |
| 5 | methods on the standard string object. They used to be implemented by |
| 6 | a built-in module 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 |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 38 | # Note that Cookie.py bogusly uses _idmap :( |
Martin v. Löwis | 5357c65 | 2002-10-14 20:03:40 +0000 | [diff] [blame] | 39 | l = map(chr, xrange(256)) |
| 40 | _idmap = str('').join(l) |
| 41 | del l |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 42 | |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 43 | # Functions which aren't available as string methods. |
| 44 | |
| 45 | # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 46 | def capwords(s, sep=None): |
| 47 | """capwords(s, [sep]) -> string |
| 48 | |
| 49 | Split the argument into words using split, capitalize each |
| 50 | word using capitalize, and join the capitalized words using |
| 51 | join. Note that this replaces runs of whitespace characters by |
| 52 | a single space. |
| 53 | |
| 54 | """ |
| 55 | return (sep or ' ').join([x.capitalize() for x in s.split(sep)]) |
| 56 | |
| 57 | |
| 58 | # Construct a translation string |
| 59 | _idmapL = None |
| 60 | def maketrans(fromstr, tostr): |
| 61 | """maketrans(frm, to) -> string |
| 62 | |
| 63 | Return a translation table (a string of 256 bytes long) |
| 64 | suitable for use in string.translate. The strings frm and to |
| 65 | must be of the same length. |
| 66 | |
| 67 | """ |
| 68 | if len(fromstr) != len(tostr): |
| 69 | raise ValueError, "maketrans arguments must have same length" |
| 70 | global _idmapL |
| 71 | if not _idmapL: |
| 72 | _idmapL = map(None, _idmap) |
| 73 | L = _idmapL[:] |
| 74 | fromstr = map(ord, fromstr) |
| 75 | for i in range(len(fromstr)): |
| 76 | L[fromstr[i]] = tostr[i] |
| 77 | return ''.join(L) |
| 78 | |
| 79 | |
Raymond Hettinger | 57aef9c | 2004-12-07 07:55:07 +0000 | [diff] [blame] | 80 | |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 81 | #################################################################### |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 82 | import re as _re |
| 83 | |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 84 | class _multimap: |
| 85 | """Helper class for combining multiple mappings. |
| 86 | |
| 87 | Used by .{safe_,}substitute() to combine the mapping and keyword |
| 88 | arguments. |
| 89 | """ |
| 90 | def __init__(self, primary, secondary): |
| 91 | self._primary = primary |
| 92 | self._secondary = secondary |
| 93 | |
| 94 | def __getitem__(self, key): |
| 95 | try: |
| 96 | return self._primary[key] |
| 97 | except KeyError: |
| 98 | return self._secondary[key] |
| 99 | |
| 100 | |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 101 | class _TemplateMetaclass(type): |
| 102 | pattern = r""" |
Raymond Hettinger | 55593c3 | 2004-09-26 18:56:44 +0000 | [diff] [blame] | 103 | %(delim)s(?: |
| 104 | (?P<escaped>%(delim)s) | # Escape sequence of two delimiters |
| 105 | (?P<named>%(id)s) | # delimiter and a Python identifier |
| 106 | {(?P<braced>%(id)s)} | # delimiter and a braced identifier |
| 107 | (?P<invalid>) # Other ill-formed delimiter exprs |
| 108 | ) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 109 | """ |
| 110 | |
| 111 | def __init__(cls, name, bases, dct): |
| 112 | super(_TemplateMetaclass, cls).__init__(name, bases, dct) |
| 113 | if 'pattern' in dct: |
| 114 | pattern = cls.pattern |
| 115 | else: |
| 116 | pattern = _TemplateMetaclass.pattern % { |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 117 | 'delim' : _re.escape(cls.delimiter), |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 118 | 'id' : cls.idpattern, |
| 119 | } |
| 120 | cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE) |
| 121 | |
| 122 | |
| 123 | class Template: |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 124 | """A string class for supporting $-substitutions.""" |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 125 | __metaclass__ = _TemplateMetaclass |
| 126 | |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 127 | delimiter = '$' |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 128 | idpattern = r'[_a-z][_a-z0-9]*' |
| 129 | |
| 130 | def __init__(self, template): |
| 131 | self.template = template |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 132 | |
| 133 | # Search for $$, $identifier, ${identifier}, and any bare $'s |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 134 | |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 135 | def _invalid(self, mo): |
| 136 | i = mo.start('invalid') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 137 | lines = self.template[:i].splitlines(True) |
| 138 | if not lines: |
| 139 | colno = 1 |
| 140 | lineno = 1 |
| 141 | else: |
| 142 | colno = i - len(''.join(lines[:-1])) |
| 143 | lineno = len(lines) |
| 144 | raise ValueError('Invalid placeholder in string: line %d, col %d' % |
| 145 | (lineno, colno)) |
| 146 | |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 147 | def substitute(self, *args, **kws): |
| 148 | if len(args) > 1: |
| 149 | raise TypeError('Too many positional arguments') |
| 150 | if not args: |
| 151 | mapping = kws |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 152 | elif kws: |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 153 | mapping = _multimap(kws, args[0]) |
| 154 | else: |
| 155 | mapping = args[0] |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 156 | # Helper function for .sub() |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 157 | def convert(mo): |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 158 | # Check the most common path first. |
| 159 | named = mo.group('named') or mo.group('braced') |
| 160 | if named is not None: |
| 161 | val = mapping[named] |
| 162 | # We use this idiom instead of str() because the latter will |
| 163 | # fail if val is a Unicode containing non-ASCII characters. |
| 164 | return '%s' % val |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 165 | if mo.group('escaped') is not None: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 166 | return self.delimiter |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 167 | if mo.group('invalid') is not None: |
| 168 | self._invalid(mo) |
Neal Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 169 | raise ValueError('Unrecognized named group in pattern', |
| 170 | self.pattern) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 171 | return self.pattern.sub(convert, self.template) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 172 | |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 173 | def safe_substitute(self, *args, **kws): |
| 174 | if len(args) > 1: |
| 175 | raise TypeError('Too many positional arguments') |
| 176 | if not args: |
| 177 | mapping = kws |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 178 | elif kws: |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 179 | mapping = _multimap(kws, args[0]) |
| 180 | else: |
| 181 | mapping = args[0] |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 182 | # Helper function for .sub() |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 183 | def convert(mo): |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 184 | named = mo.group('named') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 185 | if named is not None: |
| 186 | try: |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 187 | # We use this idiom instead of str() because the latter |
| 188 | # will fail if val is a Unicode containing non-ASCII |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 189 | return '%s' % mapping[named] |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 190 | except KeyError: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 191 | return self.delimiter + named |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 192 | braced = mo.group('braced') |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 193 | if braced is not None: |
| 194 | try: |
| 195 | return '%s' % mapping[braced] |
| 196 | except KeyError: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 197 | return self.delimiter + '{' + braced + '}' |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 198 | if mo.group('escaped') is not None: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 199 | return self.delimiter |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 200 | if mo.group('invalid') is not None: |
Barry Warsaw | 8c72eae | 2004-11-01 03:52:43 +0000 | [diff] [blame] | 201 | return self.delimiter |
Neal Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 202 | raise ValueError('Unrecognized named group in pattern', |
| 203 | self.pattern) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 204 | return self.pattern.sub(convert, self.template) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 205 | |
| 206 | |
Raymond Hettinger | 57aef9c | 2004-12-07 07:55:07 +0000 | [diff] [blame] | 207 | |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 208 | #################################################################### |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 209 | # NOTE: Everything below here is deprecated. Use string methods instead. |
| 210 | # This stuff will go away in Python 3.0. |
| 211 | |
Guido van Rossum | 710c352 | 1994-08-17 13:16:11 +0000 | [diff] [blame] | 212 | # Backward compatible names for exceptions |
| 213 | index_error = ValueError |
| 214 | atoi_error = ValueError |
| 215 | atof_error = ValueError |
| 216 | atol_error = ValueError |
| 217 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 218 | # convert UPPER CASE letters to lower case |
| 219 | def lower(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 220 | """lower(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 221 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 222 | Return a copy of the string s converted to lowercase. |
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 | """ |
| 225 | return s.lower() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 226 | |
| 227 | # Convert lower case letters to UPPER CASE |
| 228 | def upper(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 229 | """upper(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 230 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 231 | Return a copy of the string s converted to uppercase. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 232 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 233 | """ |
| 234 | return s.upper() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 235 | |
| 236 | # Swap lower case letters and UPPER CASE |
| 237 | def swapcase(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 238 | """swapcase(s) -> 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 copy of the string s with upper case characters |
| 241 | converted to lowercase and vice versa. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 242 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 243 | """ |
| 244 | return s.swapcase() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 245 | |
| 246 | # Strip leading and trailing tabs and spaces |
Martin v. Löwis | 1f04610 | 2002-11-08 12:09:59 +0000 | [diff] [blame] | 247 | def strip(s, chars=None): |
Neal Norwitz | a4864a2 | 2002-11-14 03:31:32 +0000 | [diff] [blame] | 248 | """strip(s [,chars]) -> 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 copy of the string s with leading and trailing |
| 251 | whitespace removed. |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 252 | If chars is given and not None, remove characters in chars instead. |
Neal Norwitz | a4864a2 | 2002-11-14 03:31:32 +0000 | [diff] [blame] | 253 | If chars is unicode, S will be converted to unicode before stripping. |
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 | """ |
Martin v. Löwis | 1f04610 | 2002-11-08 12:09:59 +0000 | [diff] [blame] | 256 | return s.strip(chars) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 258 | # Strip leading tabs and spaces |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 259 | def lstrip(s, chars=None): |
| 260 | """lstrip(s [,chars]) -> 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 copy of the string s with leading whitespace removed. |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 263 | If chars is given and not None, remove characters in chars instead. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 264 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 265 | """ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 266 | return s.lstrip(chars) |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 267 | |
| 268 | # Strip trailing tabs and spaces |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 269 | def rstrip(s, chars=None): |
| 270 | """rstrip(s [,chars]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 271 | |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 272 | Return a copy of the string s with trailing whitespace removed. |
| 273 | If chars is given and not None, remove characters in chars instead. |
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 | """ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 276 | return s.rstrip(chars) |
Guido van Rossum | 306a8a6 | 1996-08-08 18:40:59 +0000 | [diff] [blame] | 277 | |
| 278 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 279 | # Split a string into a list of space/tab-separated words |
Guido van Rossum | 8f0c5a7 | 2000-03-10 23:22:10 +0000 | [diff] [blame] | 280 | def split(s, sep=None, maxsplit=-1): |
Fred Drake | e4f1366 | 1999-11-04 19:19:48 +0000 | [diff] [blame] | 281 | """split(s [,sep [,maxsplit]]) -> list of strings |
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 | 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] | 284 | delimiter string. If maxsplit is given, splits at no more than |
| 285 | maxsplit places (resulting in at most maxsplit+1 words). If sep |
Walter Dörwald | 065a32f | 2004-09-14 09:45:10 +0000 | [diff] [blame] | 286 | is not specified or is None, any whitespace string is a separator. |
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 | (split and splitfields are synonymous) |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 289 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 290 | """ |
| 291 | return s.split(sep, maxsplit) |
| 292 | splitfields = split |
Guido van Rossum | fac38b7 | 1991-04-07 13:42:19 +0000 | [diff] [blame] | 293 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 294 | # Split a string into a list of space/tab-separated words |
| 295 | def rsplit(s, sep=None, maxsplit=-1): |
| 296 | """rsplit(s [,sep [,maxsplit]]) -> list of strings |
| 297 | |
| 298 | Return a list of the words in the string s, using sep as the |
| 299 | delimiter string, starting at the end of the string and working |
| 300 | to the front. If maxsplit is given, at most maxsplit splits are |
| 301 | done. If sep is not specified or is None, any whitespace string |
| 302 | is a separator. |
| 303 | """ |
| 304 | return s.rsplit(sep, maxsplit) |
| 305 | |
Guido van Rossum | 2ab1992 | 1995-06-22 18:58:00 +0000 | [diff] [blame] | 306 | # Join fields with optional separator |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 307 | def join(words, sep = ' '): |
| 308 | """join(list [,sep]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 309 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 310 | Return a string composed of the words in list, with |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 311 | intervening occurrences of sep. The default separator is a |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 312 | single space. |
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 | (joinfields and join are synonymous) |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 315 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 316 | """ |
| 317 | return sep.join(words) |
| 318 | joinfields = join |
| 319 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 320 | # Find substring, raise exception if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 321 | def index(s, *args): |
| 322 | """index(s, sub [,start [,end]]) -> int |
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 | Like find but raises ValueError when the substring is not found. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 325 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 326 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 327 | return s.index(*args) |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 328 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 329 | # Find last substring, raise exception if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 330 | def rindex(s, *args): |
| 331 | """rindex(s, sub [,start [,end]]) -> int |
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 | Like rfind but raises ValueError when the substring is not found. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 334 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 335 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 336 | return s.rindex(*args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 337 | |
| 338 | # Count non-overlapping occurrences of substring |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 339 | def count(s, *args): |
| 340 | """count(s, sub[, start[,end]]) -> int |
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 the number of occurrences of substring sub in string |
| 343 | s[start:end]. Optional arguments start and end are |
| 344 | interpreted as in slice notation. |
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 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 347 | return s.count(*args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 349 | # Find substring, return -1 if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 350 | def find(s, *args): |
| 351 | """find(s, sub [,start [,end]]) -> in |
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 | Return the lowest index in s where substring sub is found, |
| 354 | such that sub is contained within s[start,end]. Optional |
| 355 | arguments start and end are interpreted as in slice notation. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 356 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 357 | Return -1 on failure. |
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 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 360 | return s.find(*args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 361 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 362 | # Find last substring, return -1 if not found |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 363 | def rfind(s, *args): |
| 364 | """rfind(s, sub [,start [,end]]) -> int |
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 | Return the highest index in s where substring sub is found, |
| 367 | such that sub is contained within s[start,end]. Optional |
| 368 | arguments start and end are interpreted as in slice notation. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 369 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 370 | Return -1 on failure. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 371 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 372 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 373 | return s.rfind(*args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 374 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 375 | # for a bit of speed |
| 376 | _float = float |
| 377 | _int = int |
| 378 | _long = long |
Guido van Rossum | d0753e2 | 1997-12-10 22:59:55 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 380 | # Convert string to float |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 381 | def atof(s): |
| 382 | """atof(s) -> float |
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 the floating point number represented by the string s. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 385 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 386 | """ |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 387 | return _float(s) |
| 388 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 390 | # Convert string to integer |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 391 | def atoi(s , base=10): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 392 | """atoi(s [,base]) -> int |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 393 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 394 | Return the integer represented by the string s in the given |
| 395 | base, which defaults to 10. The string s must consist of one |
| 396 | or more digits, possibly preceded by a sign. If base is 0, it |
| 397 | is chosen from the leading characters of s, 0 for octal, 0x or |
| 398 | 0X for hexadecimal. If base is 16, a preceding 0x or 0X is |
| 399 | accepted. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 400 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 401 | """ |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 402 | return _int(s, base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 403 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 404 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 405 | # Convert string to long integer |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 406 | def atol(s, base=10): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 407 | """atol(s [,base]) -> long |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 408 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 409 | Return the long integer represented by the string s in the |
| 410 | given base, which defaults to 10. The string s must consist |
| 411 | of one or more digits, possibly preceded by a sign. If base |
| 412 | is 0, it is chosen from the leading characters of s, 0 for |
| 413 | octal, 0x or 0X for hexadecimal. If base is 16, a preceding |
| 414 | 0x or 0X is accepted. A trailing L or l is not accepted, |
| 415 | unless base is 0. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 416 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 417 | """ |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 418 | return _long(s, base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 421 | # Left-justify a string |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 422 | def ljust(s, width, *args): |
| 423 | """ljust(s, width[, fillchar]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 424 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 425 | Return a left-justified version of s, in a field of the |
| 426 | specified width, padded with spaces as needed. The string is |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 427 | never truncated. If specified the fillchar is used instead of spaces. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 428 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 429 | """ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 430 | return s.ljust(width, *args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 431 | |
| 432 | # Right-justify a string |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 433 | def rjust(s, width, *args): |
| 434 | """rjust(s, width[, fillchar]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 435 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 436 | Return a right-justified version of s, in a field of the |
| 437 | specified width, padded with spaces as needed. The string is |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 438 | never truncated. If specified the fillchar is used instead of spaces. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 439 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 440 | """ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 441 | return s.rjust(width, *args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 442 | |
| 443 | # Center a string |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 444 | def center(s, width, *args): |
| 445 | """center(s, width[, fillchar]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 446 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 447 | Return a center version of s, in a field of the specified |
| 448 | width. padded with spaces as needed. The string is never |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 449 | truncated. If specified the fillchar is used instead of spaces. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 450 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 451 | """ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 452 | return s.center(width, *args) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 453 | |
| 454 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
| 455 | # Decadent feature: the argument may be a string or a number |
| 456 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
| 457 | def zfill(x, width): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 458 | """zfill(x, width) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 459 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 460 | Pad a numeric string x with zeros on the left, to fill a field |
| 461 | of the specified width. The string x is never truncated. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 462 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 463 | """ |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 464 | if not isinstance(x, basestring): |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 465 | x = repr(x) |
| 466 | return x.zfill(width) |
Guido van Rossum | 6ff2e90 | 1992-03-27 15:13:31 +0000 | [diff] [blame] | 467 | |
| 468 | # Expand tabs in a string. |
| 469 | # 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] | 470 | def expandtabs(s, tabsize=8): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 471 | """expandtabs(s [,tabsize]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 472 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 473 | Return a copy of the string s with all tab characters replaced |
| 474 | by the appropriate number of spaces, depending on the current |
| 475 | column, and the tabsize (default 8). |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 476 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 477 | """ |
Fred Drake | 046d272 | 2000-07-03 07:23:13 +0000 | [diff] [blame] | 478 | return s.expandtabs(tabsize) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 479 | |
Guido van Rossum | 2539528 | 1996-05-28 23:08:45 +0000 | [diff] [blame] | 480 | # Character translation through look-up table. |
Guido van Rossum | ed7253c | 1996-07-23 18:12:39 +0000 | [diff] [blame] | 481 | def translate(s, table, deletions=""): |
Guido van Rossum | 5aff775 | 2000-12-19 02:39:08 +0000 | [diff] [blame] | 482 | """translate(s,table [,deletions]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 483 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 484 | Return a copy of the string s, where all characters occurring |
Guido van Rossum | 5aff775 | 2000-12-19 02:39:08 +0000 | [diff] [blame] | 485 | in the optional argument deletions are removed, and the |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 486 | remaining characters have been mapped through the given |
Guido van Rossum | 5aff775 | 2000-12-19 02:39:08 +0000 | [diff] [blame] | 487 | translation table, which must be a string of length 256. The |
| 488 | deletions argument is not allowed for Unicode strings. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 489 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 490 | """ |
Guido van Rossum | 5aff775 | 2000-12-19 02:39:08 +0000 | [diff] [blame] | 491 | if deletions: |
| 492 | return s.translate(table, deletions) |
| 493 | else: |
| 494 | # Add s[:0] so that if s is Unicode and table is an 8-bit string, |
| 495 | # table is converted to Unicode. This means that table *cannot* |
| 496 | # be a dictionary -- for that feature, use u.translate() directly. |
| 497 | return s.translate(table + s[:0]) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 498 | |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 499 | # Capitalize a string, e.g. "aBc dEf" -> "Abc def". |
| 500 | def capitalize(s): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 501 | """capitalize(s) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 502 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 503 | Return a copy of the string s with only its first character |
| 504 | capitalized. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 505 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 506 | """ |
| 507 | return s.capitalize() |
Guido van Rossum | 8775d8b | 1996-06-11 18:43:00 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | 1eb9a81 | 1997-03-25 16:50:31 +0000 | [diff] [blame] | 509 | # Substring replacement (global) |
Guido van Rossum | 8f0c5a7 | 2000-03-10 23:22:10 +0000 | [diff] [blame] | 510 | def replace(s, old, new, maxsplit=-1): |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 511 | """replace (str, old, new[, maxsplit]) -> string |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 512 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 513 | Return a copy of string str with all occurrences of substring |
| 514 | old replaced by new. If the optional argument maxsplit is |
| 515 | given, only the first maxsplit occurrences are replaced. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 516 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 517 | """ |
| 518 | return s.replace(old, new, maxsplit) |
Guido van Rossum | 1eb9a81 | 1997-03-25 16:50:31 +0000 | [diff] [blame] | 519 | |
| 520 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 521 | # Try importing optional built-in module "strop" -- if it exists, |
| 522 | # it redefines some string operations that are 100-1000 times faster. |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 523 | # It also defines values for whitespace, lowercase and uppercase |
| 524 | # that match <ctype.h>'s definitions. |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 525 | |
| 526 | try: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 527 | from strop import maketrans, lowercase, uppercase, whitespace |
| 528 | letters = lowercase + uppercase |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 529 | except ImportError: |
Fred Drake | 857c4c3 | 2000-02-10 16:21:11 +0000 | [diff] [blame] | 530 | pass # Use the original versions |