blob: d682fc7abcfed2bcbc5461583445cc2a201bb188 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""A collection of string operations (most are no longer used in Python 1.6).
Guido van Rossumc6360141990-10-13 19:23:40 +00002
Guido van Rossume7b146f2000-02-04 15:28:42 +00003Warning: most of the code you see here isn't normally used nowadays. With
4Python 1.6, many of these functions are implemented as methods on the
5standard string object. They used to be implemented by a built-in module
6called strop, but strop is now obsolete itself.
Guido van Rossum20032041997-12-29 19:26:28 +00007
8Public module variables:
9
10whitespace -- a string containing all characters considered whitespace
11lowercase -- a string containing all characters considered lowercase letters
12uppercase -- a string containing all characters considered uppercase letters
13letters -- a string containing all characters considered letters
14digits -- a string containing all characters considered decimal digits
15hexdigits -- a string containing all characters considered hexadecimal digits
16octdigits -- a string containing all characters considered octal digits
Fred Drakefd64c592000-09-18 19:38:11 +000017punctuation -- a string containing all characters considered punctuation
18printable -- a string containing all characters considered printable
Guido van Rossum20032041997-12-29 19:26:28 +000019
20"""
21
Guido van Rossumc6360141990-10-13 19:23:40 +000022# Some strings for ctype-style character classification
Guido van Rossum8e2ec561993-07-29 09:37:38 +000023whitespace = ' \t\n\r\v\f'
Guido van Rossumc6360141990-10-13 19:23:40 +000024lowercase = 'abcdefghijklmnopqrstuvwxyz'
25uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
26letters = lowercase + uppercase
Fred Drake960fdf92001-07-20 18:38:26 +000027ascii_lowercase = lowercase
28ascii_uppercase = uppercase
29ascii_letters = ascii_lowercase + ascii_uppercase
Guido van Rossumc6360141990-10-13 19:23:40 +000030digits = '0123456789'
31hexdigits = digits + 'abcdef' + 'ABCDEF'
32octdigits = '01234567'
Tim Peters495ad3c2001-01-15 01:36:40 +000033punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
Fred Drake6b2320f2000-09-18 16:46:17 +000034printable = digits + letters + punctuation + whitespace
Guido van Rossumc6360141990-10-13 19:23:40 +000035
36# Case conversion helpers
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000037_idmap = ''
38for i in range(256): _idmap = _idmap + chr(i)
Guido van Rossumc6360141990-10-13 19:23:40 +000039del i
40
Guido van Rossum710c3521994-08-17 13:16:11 +000041# Backward compatible names for exceptions
42index_error = ValueError
43atoi_error = ValueError
44atof_error = ValueError
45atol_error = ValueError
46
Guido van Rossumc6360141990-10-13 19:23:40 +000047# convert UPPER CASE letters to lower case
48def lower(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000049 """lower(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000050
Barry Warsaw226ae6c1999-10-12 19:54:53 +000051 Return a copy of the string s converted to lowercase.
Guido van Rossum20032041997-12-29 19:26:28 +000052
Barry Warsaw226ae6c1999-10-12 19:54:53 +000053 """
54 return s.lower()
Guido van Rossumc6360141990-10-13 19:23:40 +000055
56# Convert lower case letters to UPPER CASE
57def upper(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000058 """upper(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000059
Barry Warsaw226ae6c1999-10-12 19:54:53 +000060 Return a copy of the string s converted to uppercase.
Guido van Rossum20032041997-12-29 19:26:28 +000061
Barry Warsaw226ae6c1999-10-12 19:54:53 +000062 """
63 return s.upper()
Guido van Rossumc6360141990-10-13 19:23:40 +000064
65# Swap lower case letters and UPPER CASE
66def swapcase(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000067 """swapcase(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000068
Barry Warsaw226ae6c1999-10-12 19:54:53 +000069 Return a copy of the string s with upper case characters
70 converted to lowercase and vice versa.
Guido van Rossum20032041997-12-29 19:26:28 +000071
Barry Warsaw226ae6c1999-10-12 19:54:53 +000072 """
73 return s.swapcase()
Guido van Rossumc6360141990-10-13 19:23:40 +000074
75# Strip leading and trailing tabs and spaces
76def strip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000077 """strip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000078
Barry Warsaw226ae6c1999-10-12 19:54:53 +000079 Return a copy of the string s with leading and trailing
80 whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000081
Barry Warsaw226ae6c1999-10-12 19:54:53 +000082 """
83 return s.strip()
Guido van Rossumc6360141990-10-13 19:23:40 +000084
Guido van Rossum306a8a61996-08-08 18:40:59 +000085# Strip leading tabs and spaces
86def lstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000087 """lstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000088
Barry Warsaw226ae6c1999-10-12 19:54:53 +000089 Return a copy of the string s with leading whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000090
Barry Warsaw226ae6c1999-10-12 19:54:53 +000091 """
92 return s.lstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000093
94# Strip trailing tabs and spaces
95def rstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000096 """rstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000097
Barry Warsaw226ae6c1999-10-12 19:54:53 +000098 Return a copy of the string s with trailing whitespace
99 removed.
Guido van Rossum20032041997-12-29 19:26:28 +0000100
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000101 """
102 return s.rstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +0000103
104
Guido van Rossumc6360141990-10-13 19:23:40 +0000105# Split a string into a list of space/tab-separated words
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000106def split(s, sep=None, maxsplit=-1):
Fred Drakee4f13661999-11-04 19:19:48 +0000107 """split(s [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000108
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000109 Return a list of the words in the string s, using sep as the
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000110 delimiter string. If maxsplit is given, splits into at most
Fred Drakee4f13661999-11-04 19:19:48 +0000111 maxsplit words. If sep is not specified, any whitespace string
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000112 is a separator.
Guido van Rossum20032041997-12-29 19:26:28 +0000113
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000114 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000115
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000116 """
117 return s.split(sep, maxsplit)
118splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000119
Guido van Rossum2ab19921995-06-22 18:58:00 +0000120# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000121def join(words, sep = ' '):
122 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000123
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000124 Return a string composed of the words in list, with
Thomas Wouters7e474022000-07-16 12:04:32 +0000125 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000126 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000127
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000128 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000129
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000130 """
131 return sep.join(words)
132joinfields = join
133
Guido van Rossumd3166071993-05-24 14:16:22 +0000134# Find substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000135def index(s, *args):
136 """index(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000137
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000138 Like find but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000139
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000140 """
Fred Drake046d2722000-07-03 07:23:13 +0000141 return s.index(*args)
Guido van Rossumd3166071993-05-24 14:16:22 +0000142
Guido van Rossume65cce51993-11-08 15:05:21 +0000143# Find last substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000144def rindex(s, *args):
145 """rindex(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000146
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000147 Like rfind but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000148
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000149 """
Fred Drake046d2722000-07-03 07:23:13 +0000150 return s.rindex(*args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000151
152# Count non-overlapping occurrences of substring
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000153def count(s, *args):
154 """count(s, sub[, start[,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000155
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000156 Return the number of occurrences of substring sub in string
157 s[start:end]. Optional arguments start and end are
158 interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000159
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000160 """
Fred Drake046d2722000-07-03 07:23:13 +0000161 return s.count(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000162
Guido van Rossumd3166071993-05-24 14:16:22 +0000163# Find substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000164def find(s, *args):
165 """find(s, sub [,start [,end]]) -> in
Guido van Rossum20032041997-12-29 19:26:28 +0000166
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000167 Return the lowest index in s where substring sub is found,
168 such that sub is contained within s[start,end]. Optional
169 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000170
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000171 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000172
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000173 """
Fred Drake046d2722000-07-03 07:23:13 +0000174 return s.find(*args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000175
Guido van Rossume65cce51993-11-08 15:05:21 +0000176# Find last substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000177def rfind(s, *args):
178 """rfind(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000179
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000180 Return the highest index in s where substring sub is found,
181 such that sub is contained within s[start,end]. Optional
182 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000183
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000184 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000185
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000186 """
Fred Drake046d2722000-07-03 07:23:13 +0000187 return s.rfind(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000188
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000189# for a bit of speed
190_float = float
191_int = int
192_long = long
193_StringType = type('')
Guido van Rossumd0753e21997-12-10 22:59:55 +0000194
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000195# Convert string to float
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000196def atof(s):
197 """atof(s) -> float
Guido van Rossum20032041997-12-29 19:26:28 +0000198
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000199 Return the floating point number represented by the string s.
Guido van Rossum20032041997-12-29 19:26:28 +0000200
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000201 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000202 return _float(s)
203
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000204
Guido van Rossumc6360141990-10-13 19:23:40 +0000205# Convert string to integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000206def atoi(s , base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000207 """atoi(s [,base]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000208
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000209 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 Rossum20032041997-12-29 19:26:28 +0000215
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000216 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000217 return _int(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000218
Guido van Rossumc6360141990-10-13 19:23:40 +0000219
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000220# Convert string to long integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000221def atol(s, base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000222 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000223
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000224 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 Rossum20032041997-12-29 19:26:28 +0000231
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000232 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000233 return _long(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000234
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000235
Guido van Rossumc6360141990-10-13 19:23:40 +0000236# Left-justify a string
237def ljust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000238 """ljust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000239
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000240 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 Rossum20032041997-12-29 19:26:28 +0000243
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000244 """
Fred Drake046d2722000-07-03 07:23:13 +0000245 return s.ljust(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000246
247# Right-justify a string
248def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000249 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000250
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000251 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 Rossum20032041997-12-29 19:26:28 +0000254
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000255 """
Fred Drake046d2722000-07-03 07:23:13 +0000256 return s.rjust(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000257
258# Center a string
259def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000260 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000261
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000262 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 Rossum20032041997-12-29 19:26:28 +0000265
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000266 """
Fred Drake046d2722000-07-03 07:23:13 +0000267 return s.center(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000268
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.)
272def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000273 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000274
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000275 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 Rossum20032041997-12-29 19:26:28 +0000277
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000278 """
279 if type(x) == type(''): s = x
280 else: s = `x`
281 n = len(s)
282 if n >= width: return s
283 sign = ''
284 if s[0] in ('-', '+'):
Fred Drake857c4c32000-02-10 16:21:11 +0000285 sign, s = s[0], s[1:]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000286 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000287
288# Expand tabs in a string.
289# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000290def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000291 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000292
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000293 Return a copy of the string s with all tab characters replaced
294 by the appropriate number of spaces, depending on the current
295 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000296
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000297 """
Fred Drake046d2722000-07-03 07:23:13 +0000298 return s.expandtabs(tabsize)
Guido van Rossum2db91351992-10-18 17:09:59 +0000299
Guido van Rossum25395281996-05-28 23:08:45 +0000300# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000301def translate(s, table, deletions=""):
Guido van Rossum5aff7752000-12-19 02:39:08 +0000302 """translate(s,table [,deletions]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000303
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000304 Return a copy of the string s, where all characters occurring
Guido van Rossum5aff7752000-12-19 02:39:08 +0000305 in the optional argument deletions are removed, and the
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000306 remaining characters have been mapped through the given
Guido van Rossum5aff7752000-12-19 02:39:08 +0000307 translation table, which must be a string of length 256. The
308 deletions argument is not allowed for Unicode strings.
Guido van Rossum20032041997-12-29 19:26:28 +0000309
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000310 """
Guido van Rossum5aff7752000-12-19 02:39:08 +0000311 if deletions:
312 return s.translate(table, deletions)
313 else:
314 # Add s[:0] so that if s is Unicode and table is an 8-bit string,
315 # table is converted to Unicode. This means that table *cannot*
316 # be a dictionary -- for that feature, use u.translate() directly.
317 return s.translate(table + s[:0])
Guido van Rossum2db91351992-10-18 17:09:59 +0000318
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000319# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
320def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000321 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000322
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000323 Return a copy of the string s with only its first character
324 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000325
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000326 """
327 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000328
329# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
330# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000331def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000332 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000333
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000334 Split the argument into words using split, capitalize each
335 word using capitalize, and join the capitalized words using
336 join. Note that this replaces runs of whitespace characters by
337 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000338
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000339 """
340 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000341
Guido van Rossumed7253c1996-07-23 18:12:39 +0000342# Construct a translation string
343_idmapL = None
344def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000345 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000346
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000347 Return a translation table (a string of 256 bytes long)
348 suitable for use in string.translate. The strings frm and to
349 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000350
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000351 """
352 if len(fromstr) != len(tostr):
Fred Drake857c4c32000-02-10 16:21:11 +0000353 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000354 global _idmapL
355 if not _idmapL:
Fred Drake857c4c32000-02-10 16:21:11 +0000356 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000357 L = _idmapL[:]
358 fromstr = map(ord, fromstr)
359 for i in range(len(fromstr)):
Fred Drake857c4c32000-02-10 16:21:11 +0000360 L[fromstr[i]] = tostr[i]
Eric S. Raymonde37340e2001-02-09 16:56:44 +0000361 return join(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000362
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000363# Substring replacement (global)
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000364def replace(s, old, new, maxsplit=-1):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000365 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000366
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000367 Return a copy of string str with all occurrences of substring
368 old replaced by new. If the optional argument maxsplit is
369 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000370
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000371 """
372 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000373
374
Guido van Rossum2db91351992-10-18 17:09:59 +0000375# Try importing optional built-in module "strop" -- if it exists,
376# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000377# It also defines values for whitespace, lowercase and uppercase
378# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000379
380try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000381 from strop import maketrans, lowercase, uppercase, whitespace
382 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000383except ImportError:
Fred Drake857c4c32000-02-10 16:21:11 +0000384 pass # Use the original versions