blob: 64e1190496068265b7aa3d4bc5667137ed3fd452 [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
Martin v. Löwis5357c652002-10-14 20:03:40 +000037# Use str to convert Unicode literal in case of -U
38l = map(chr, xrange(256))
39_idmap = str('').join(l)
40del l
Guido van Rossumc6360141990-10-13 19:23:40 +000041
Guido van Rossum710c3521994-08-17 13:16:11 +000042# Backward compatible names for exceptions
43index_error = ValueError
44atoi_error = ValueError
45atof_error = ValueError
46atol_error = ValueError
47
Guido van Rossumc6360141990-10-13 19:23:40 +000048# convert UPPER CASE letters to lower case
49def lower(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000050 """lower(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000051
Barry Warsaw226ae6c1999-10-12 19:54:53 +000052 Return a copy of the string s converted to lowercase.
Guido van Rossum20032041997-12-29 19:26:28 +000053
Barry Warsaw226ae6c1999-10-12 19:54:53 +000054 """
55 return s.lower()
Guido van Rossumc6360141990-10-13 19:23:40 +000056
57# Convert lower case letters to UPPER CASE
58def upper(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000059 """upper(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000060
Barry Warsaw226ae6c1999-10-12 19:54:53 +000061 Return a copy of the string s converted to uppercase.
Guido van Rossum20032041997-12-29 19:26:28 +000062
Barry Warsaw226ae6c1999-10-12 19:54:53 +000063 """
64 return s.upper()
Guido van Rossumc6360141990-10-13 19:23:40 +000065
66# Swap lower case letters and UPPER CASE
67def swapcase(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000068 """swapcase(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000069
Barry Warsaw226ae6c1999-10-12 19:54:53 +000070 Return a copy of the string s with upper case characters
71 converted to lowercase and vice versa.
Guido van Rossum20032041997-12-29 19:26:28 +000072
Barry Warsaw226ae6c1999-10-12 19:54:53 +000073 """
74 return s.swapcase()
Guido van Rossumc6360141990-10-13 19:23:40 +000075
76# Strip leading and trailing tabs and spaces
Martin v. Löwis1f046102002-11-08 12:09:59 +000077def strip(s, chars=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000078 """strip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000079
Barry Warsaw226ae6c1999-10-12 19:54:53 +000080 Return a copy of the string s with leading and trailing
81 whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000082
Barry Warsaw226ae6c1999-10-12 19:54:53 +000083 """
Martin v. Löwis1f046102002-11-08 12:09:59 +000084 return s.strip(chars)
Guido van Rossumc6360141990-10-13 19:23:40 +000085
Guido van Rossum306a8a61996-08-08 18:40:59 +000086# Strip leading tabs and spaces
87def lstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000088 """lstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000089
Barry Warsaw226ae6c1999-10-12 19:54:53 +000090 Return a copy of the string s with leading whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000091
Barry Warsaw226ae6c1999-10-12 19:54:53 +000092 """
93 return s.lstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000094
95# Strip trailing tabs and spaces
96def rstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000097 """rstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000098
Barry Warsaw226ae6c1999-10-12 19:54:53 +000099 Return a copy of the string s with trailing whitespace
100 removed.
Guido van Rossum20032041997-12-29 19:26:28 +0000101
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000102 """
103 return s.rstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +0000104
105
Guido van Rossumc6360141990-10-13 19:23:40 +0000106# Split a string into a list of space/tab-separated words
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000107def split(s, sep=None, maxsplit=-1):
Fred Drakee4f13661999-11-04 19:19:48 +0000108 """split(s [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000109
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000110 Return a list of the words in the string s, using sep as the
Fred Drake14537542002-01-30 16:15:13 +0000111 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 Rossum20032041997-12-29 19:26:28 +0000114
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000115 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000116
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000117 """
118 return s.split(sep, maxsplit)
119splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000120
Guido van Rossum2ab19921995-06-22 18:58:00 +0000121# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000122def join(words, sep = ' '):
123 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000124
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000125 Return a string composed of the words in list, with
Thomas Wouters7e474022000-07-16 12:04:32 +0000126 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000127 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000128
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000129 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000130
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000131 """
132 return sep.join(words)
133joinfields = join
134
Guido van Rossumd3166071993-05-24 14:16:22 +0000135# Find substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000136def index(s, *args):
137 """index(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000138
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000139 Like find but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000140
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000141 """
Fred Drake046d2722000-07-03 07:23:13 +0000142 return s.index(*args)
Guido van Rossumd3166071993-05-24 14:16:22 +0000143
Guido van Rossume65cce51993-11-08 15:05:21 +0000144# Find last substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000145def rindex(s, *args):
146 """rindex(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000147
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000148 Like rfind but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000149
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000150 """
Fred Drake046d2722000-07-03 07:23:13 +0000151 return s.rindex(*args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152
153# Count non-overlapping occurrences of substring
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000154def count(s, *args):
155 """count(s, sub[, start[,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000156
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000157 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 Rossum20032041997-12-29 19:26:28 +0000160
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000161 """
Fred Drake046d2722000-07-03 07:23:13 +0000162 return s.count(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000163
Guido van Rossumd3166071993-05-24 14:16:22 +0000164# Find substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000165def find(s, *args):
166 """find(s, sub [,start [,end]]) -> in
Guido van Rossum20032041997-12-29 19:26:28 +0000167
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000168 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 Rossum20032041997-12-29 19:26:28 +0000171
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000172 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000173
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000174 """
Fred Drake046d2722000-07-03 07:23:13 +0000175 return s.find(*args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000176
Guido van Rossume65cce51993-11-08 15:05:21 +0000177# Find last substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178def rfind(s, *args):
179 """rfind(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000180
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000181 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 Rossum20032041997-12-29 19:26:28 +0000184
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000185 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000186
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000187 """
Fred Drake046d2722000-07-03 07:23:13 +0000188 return s.rfind(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000189
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000190# for a bit of speed
191_float = float
192_int = int
193_long = long
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 """
Walter Dörwald65230a22002-06-03 15:58:32 +0000279 if not isinstance(x, basestring):
Walter Dörwald068325e2002-04-15 13:36:47 +0000280 x = repr(x)
281 return x.zfill(width)
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000282
283# Expand tabs in a string.
284# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000285def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000286 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000287
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000288 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 Rossum20032041997-12-29 19:26:28 +0000291
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000292 """
Fred Drake046d2722000-07-03 07:23:13 +0000293 return s.expandtabs(tabsize)
Guido van Rossum2db91351992-10-18 17:09:59 +0000294
Guido van Rossum25395281996-05-28 23:08:45 +0000295# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000296def translate(s, table, deletions=""):
Guido van Rossum5aff7752000-12-19 02:39:08 +0000297 """translate(s,table [,deletions]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000298
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000299 Return a copy of the string s, where all characters occurring
Guido van Rossum5aff7752000-12-19 02:39:08 +0000300 in the optional argument deletions are removed, and the
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000301 remaining characters have been mapped through the given
Guido van Rossum5aff7752000-12-19 02:39:08 +0000302 translation table, which must be a string of length 256. The
303 deletions argument is not allowed for Unicode strings.
Guido van Rossum20032041997-12-29 19:26:28 +0000304
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000305 """
Guido van Rossum5aff7752000-12-19 02:39:08 +0000306 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 Rossum2db91351992-10-18 17:09:59 +0000313
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000314# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
315def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000316 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000317
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000318 Return a copy of the string s with only its first character
319 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000320
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000321 """
322 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000323
324# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
325# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000326def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000327 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000328
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000329 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 Rossum20032041997-12-29 19:26:28 +0000333
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000334 """
335 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000336
Guido van Rossumed7253c1996-07-23 18:12:39 +0000337# Construct a translation string
338_idmapL = None
339def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000340 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000341
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000342 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 Rossum20032041997-12-29 19:26:28 +0000345
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000346 """
347 if len(fromstr) != len(tostr):
Fred Drake857c4c32000-02-10 16:21:11 +0000348 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000349 global _idmapL
350 if not _idmapL:
Fred Drake857c4c32000-02-10 16:21:11 +0000351 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000352 L = _idmapL[:]
353 fromstr = map(ord, fromstr)
354 for i in range(len(fromstr)):
Fred Drake857c4c32000-02-10 16:21:11 +0000355 L[fromstr[i]] = tostr[i]
Eric S. Raymonde37340e2001-02-09 16:56:44 +0000356 return join(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000357
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000358# Substring replacement (global)
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000359def replace(s, old, new, maxsplit=-1):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000360 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000361
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000362 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 Rossum20032041997-12-29 19:26:28 +0000365
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000366 """
367 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000368
369
Guido van Rossum2db91351992-10-18 17:09:59 +0000370# Try importing optional built-in module "strop" -- if it exists,
371# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000372# It also defines values for whitespace, lowercase and uppercase
373# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000374
375try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000376 from strop import maketrans, lowercase, uppercase, whitespace
377 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000378except ImportError:
Fred Drake857c4c32000-02-10 16:21:11 +0000379 pass # Use the original versions