blob: 913d980bae6df9425f1c1cda80fd5de6dfbeb18f [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
27digits = '0123456789'
28hexdigits = digits + 'abcdef' + 'ABCDEF'
29octdigits = '01234567'
Tim Peters495ad3c2001-01-15 01:36:40 +000030punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
Fred Drake6b2320f2000-09-18 16:46:17 +000031printable = digits + letters + punctuation + whitespace
Guido van Rossumc6360141990-10-13 19:23:40 +000032
33# Case conversion helpers
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000034_idmap = ''
35for i in range(256): _idmap = _idmap + chr(i)
Guido van Rossumc6360141990-10-13 19:23:40 +000036del i
37
Guido van Rossum710c3521994-08-17 13:16:11 +000038# Backward compatible names for exceptions
39index_error = ValueError
40atoi_error = ValueError
41atof_error = ValueError
42atol_error = ValueError
43
Guido van Rossumc6360141990-10-13 19:23:40 +000044# convert UPPER CASE letters to lower case
45def lower(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000046 """lower(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000047
Barry Warsaw226ae6c1999-10-12 19:54:53 +000048 Return a copy of the string s converted to lowercase.
Guido van Rossum20032041997-12-29 19:26:28 +000049
Barry Warsaw226ae6c1999-10-12 19:54:53 +000050 """
51 return s.lower()
Guido van Rossumc6360141990-10-13 19:23:40 +000052
53# Convert lower case letters to UPPER CASE
54def upper(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000055 """upper(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000056
Barry Warsaw226ae6c1999-10-12 19:54:53 +000057 Return a copy of the string s converted to uppercase.
Guido van Rossum20032041997-12-29 19:26:28 +000058
Barry Warsaw226ae6c1999-10-12 19:54:53 +000059 """
60 return s.upper()
Guido van Rossumc6360141990-10-13 19:23:40 +000061
62# Swap lower case letters and UPPER CASE
63def swapcase(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000064 """swapcase(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000065
Barry Warsaw226ae6c1999-10-12 19:54:53 +000066 Return a copy of the string s with upper case characters
67 converted to lowercase and vice versa.
Guido van Rossum20032041997-12-29 19:26:28 +000068
Barry Warsaw226ae6c1999-10-12 19:54:53 +000069 """
70 return s.swapcase()
Guido van Rossumc6360141990-10-13 19:23:40 +000071
72# Strip leading and trailing tabs and spaces
73def strip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000074 """strip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000075
Barry Warsaw226ae6c1999-10-12 19:54:53 +000076 Return a copy of the string s with leading and trailing
77 whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000078
Barry Warsaw226ae6c1999-10-12 19:54:53 +000079 """
80 return s.strip()
Guido van Rossumc6360141990-10-13 19:23:40 +000081
Guido van Rossum306a8a61996-08-08 18:40:59 +000082# Strip leading tabs and spaces
83def lstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000084 """lstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000085
Barry Warsaw226ae6c1999-10-12 19:54:53 +000086 Return a copy of the string s with leading whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000087
Barry Warsaw226ae6c1999-10-12 19:54:53 +000088 """
89 return s.lstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000090
91# Strip trailing tabs and spaces
92def rstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000093 """rstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000094
Barry Warsaw226ae6c1999-10-12 19:54:53 +000095 Return a copy of the string s with trailing whitespace
96 removed.
Guido van Rossum20032041997-12-29 19:26:28 +000097
Barry Warsaw226ae6c1999-10-12 19:54:53 +000098 """
99 return s.rstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +0000100
101
Guido van Rossumc6360141990-10-13 19:23:40 +0000102# Split a string into a list of space/tab-separated words
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000103def split(s, sep=None, maxsplit=-1):
Fred Drakee4f13661999-11-04 19:19:48 +0000104 """split(s [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000105
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000106 Return a list of the words in the string s, using sep as the
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000107 delimiter string. If maxsplit is given, splits into at most
Fred Drakee4f13661999-11-04 19:19:48 +0000108 maxsplit words. If sep is not specified, any whitespace string
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000109 is a separator.
Guido van Rossum20032041997-12-29 19:26:28 +0000110
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000111 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000112
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000113 """
114 return s.split(sep, maxsplit)
115splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000116
Guido van Rossum2ab19921995-06-22 18:58:00 +0000117# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000118def join(words, sep = ' '):
119 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000120
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000121 Return a string composed of the words in list, with
Thomas Wouters7e474022000-07-16 12:04:32 +0000122 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000123 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000124
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000125 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000126
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000127 """
128 return sep.join(words)
129joinfields = join
130
Guido van Rossumd3166071993-05-24 14:16:22 +0000131# Find substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000132def index(s, *args):
133 """index(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000134
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000135 Like find but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000136
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000137 """
Fred Drake046d2722000-07-03 07:23:13 +0000138 return s.index(*args)
Guido van Rossumd3166071993-05-24 14:16:22 +0000139
Guido van Rossume65cce51993-11-08 15:05:21 +0000140# Find last substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000141def rindex(s, *args):
142 """rindex(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000143
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000144 Like rfind but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000145
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000146 """
Fred Drake046d2722000-07-03 07:23:13 +0000147 return s.rindex(*args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148
149# Count non-overlapping occurrences of substring
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000150def count(s, *args):
151 """count(s, sub[, start[,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000152
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000153 Return the number of occurrences of substring sub in string
154 s[start:end]. Optional arguments start and end are
155 interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000156
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000157 """
Fred Drake046d2722000-07-03 07:23:13 +0000158 return s.count(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000159
Guido van Rossumd3166071993-05-24 14:16:22 +0000160# Find substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000161def find(s, *args):
162 """find(s, sub [,start [,end]]) -> in
Guido van Rossum20032041997-12-29 19:26:28 +0000163
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000164 Return the lowest index in s where substring sub is found,
165 such that sub is contained within s[start,end]. Optional
166 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000167
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000168 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000169
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000170 """
Fred Drake046d2722000-07-03 07:23:13 +0000171 return s.find(*args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000172
Guido van Rossume65cce51993-11-08 15:05:21 +0000173# Find last substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000174def rfind(s, *args):
175 """rfind(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000176
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000177 Return the highest index in s where substring sub is found,
178 such that sub is contained within s[start,end]. Optional
179 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000180
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000181 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000182
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000183 """
Fred Drake046d2722000-07-03 07:23:13 +0000184 return s.rfind(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000185
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000186# for a bit of speed
187_float = float
188_int = int
189_long = long
190_StringType = type('')
Guido van Rossumd0753e21997-12-10 22:59:55 +0000191
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000192# Convert string to float
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000193def atof(s):
194 """atof(s) -> float
Guido van Rossum20032041997-12-29 19:26:28 +0000195
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000196 Return the floating point number represented by the string s.
Guido van Rossum20032041997-12-29 19:26:28 +0000197
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000198 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000199 return _float(s)
200
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000201
Guido van Rossumc6360141990-10-13 19:23:40 +0000202# Convert string to integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000203def atoi(s , base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000204 """atoi(s [,base]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000205
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000206 Return the integer represented by the string s in the given
207 base, which defaults to 10. The string s must consist of one
208 or more digits, possibly preceded by a sign. If base is 0, it
209 is chosen from the leading characters of s, 0 for octal, 0x or
210 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
211 accepted.
Guido van Rossum20032041997-12-29 19:26:28 +0000212
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000213 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000214 return _int(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000215
Guido van Rossumc6360141990-10-13 19:23:40 +0000216
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000217# Convert string to long integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000218def atol(s, base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000219 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000220
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000221 Return the long integer represented by the string s in the
222 given base, which defaults to 10. The string s must consist
223 of one or more digits, possibly preceded by a sign. If base
224 is 0, it is chosen from the leading characters of s, 0 for
225 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
226 0x or 0X is accepted. A trailing L or l is not accepted,
227 unless base is 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000228
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000229 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000230 return _long(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000231
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000232
Guido van Rossumc6360141990-10-13 19:23:40 +0000233# Left-justify a string
234def ljust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000235 """ljust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000236
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000237 Return a left-justified version of s, in a field of the
238 specified width, padded with spaces as needed. The string is
239 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000240
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000241 """
Fred Drake046d2722000-07-03 07:23:13 +0000242 return s.ljust(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000243
244# Right-justify a string
245def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000246 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000247
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000248 Return a right-justified version of s, in a field of the
249 specified width, padded with spaces as needed. The string is
250 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000251
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000252 """
Fred Drake046d2722000-07-03 07:23:13 +0000253 return s.rjust(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000254
255# Center a string
256def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000257 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000258
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000259 Return a center version of s, in a field of the specified
260 width. padded with spaces as needed. The string is never
261 truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000262
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000263 """
Fred Drake046d2722000-07-03 07:23:13 +0000264 return s.center(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000265
266# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
267# Decadent feature: the argument may be a string or a number
268# (Use of this is deprecated; it should be a string as with ljust c.s.)
269def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000270 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000271
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000272 Pad a numeric string x with zeros on the left, to fill a field
273 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000274
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000275 """
276 if type(x) == type(''): s = x
277 else: s = `x`
278 n = len(s)
279 if n >= width: return s
280 sign = ''
281 if s[0] in ('-', '+'):
Fred Drake857c4c32000-02-10 16:21:11 +0000282 sign, s = s[0], s[1:]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000283 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000284
285# Expand tabs in a string.
286# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000287def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000288 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000289
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000290 Return a copy of the string s with all tab characters replaced
291 by the appropriate number of spaces, depending on the current
292 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000293
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000294 """
Fred Drake046d2722000-07-03 07:23:13 +0000295 return s.expandtabs(tabsize)
Guido van Rossum2db91351992-10-18 17:09:59 +0000296
Guido van Rossum25395281996-05-28 23:08:45 +0000297# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000298def translate(s, table, deletions=""):
Guido van Rossum5aff7752000-12-19 02:39:08 +0000299 """translate(s,table [,deletions]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000300
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000301 Return a copy of the string s, where all characters occurring
Guido van Rossum5aff7752000-12-19 02:39:08 +0000302 in the optional argument deletions are removed, and the
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000303 remaining characters have been mapped through the given
Guido van Rossum5aff7752000-12-19 02:39:08 +0000304 translation table, which must be a string of length 256. The
305 deletions argument is not allowed for Unicode strings.
Guido van Rossum20032041997-12-29 19:26:28 +0000306
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000307 """
Guido van Rossum5aff7752000-12-19 02:39:08 +0000308 if deletions:
309 return s.translate(table, deletions)
310 else:
311 # Add s[:0] so that if s is Unicode and table is an 8-bit string,
312 # table is converted to Unicode. This means that table *cannot*
313 # be a dictionary -- for that feature, use u.translate() directly.
314 return s.translate(table + s[:0])
Guido van Rossum2db91351992-10-18 17:09:59 +0000315
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000316# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
317def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000318 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000319
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000320 Return a copy of the string s with only its first character
321 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000322
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000323 """
324 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000325
326# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
327# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000328def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000329 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000330
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000331 Split the argument into words using split, capitalize each
332 word using capitalize, and join the capitalized words using
333 join. Note that this replaces runs of whitespace characters by
334 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000335
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000336 """
337 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000338
Guido van Rossumed7253c1996-07-23 18:12:39 +0000339# Construct a translation string
340_idmapL = None
341def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000342 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000343
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344 Return a translation table (a string of 256 bytes long)
345 suitable for use in string.translate. The strings frm and to
346 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000347
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000348 """
349 if len(fromstr) != len(tostr):
Fred Drake857c4c32000-02-10 16:21:11 +0000350 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000351 global _idmapL
352 if not _idmapL:
Fred Drake857c4c32000-02-10 16:21:11 +0000353 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000354 L = _idmapL[:]
355 fromstr = map(ord, fromstr)
356 for i in range(len(fromstr)):
Fred Drake857c4c32000-02-10 16:21:11 +0000357 L[fromstr[i]] = tostr[i]
Eric S. Raymonde37340e2001-02-09 16:56:44 +0000358 return join(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000359
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000360# Substring replacement (global)
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000361def replace(s, old, new, maxsplit=-1):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000362 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000363
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000364 Return a copy of string str with all occurrences of substring
365 old replaced by new. If the optional argument maxsplit is
366 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000367
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000368 """
369 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000370
371
Guido van Rossum2db91351992-10-18 17:09:59 +0000372# Try importing optional built-in module "strop" -- if it exists,
373# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000374# It also defines values for whitespace, lowercase and uppercase
375# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000376
377try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000378 from strop import maketrans, lowercase, uppercase, whitespace
379 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000380except ImportError:
Fred Drake857c4c32000-02-10 16:21:11 +0000381 pass # Use the original versions