blob: 5f90723ad013315a98125e186e51b9414bc2f6b6 [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
17
18"""
19
Guido van Rossumc6360141990-10-13 19:23:40 +000020# Some strings for ctype-style character classification
Guido van Rossum8e2ec561993-07-29 09:37:38 +000021whitespace = ' \t\n\r\v\f'
Guido van Rossumc6360141990-10-13 19:23:40 +000022lowercase = 'abcdefghijklmnopqrstuvwxyz'
23uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
24letters = lowercase + uppercase
25digits = '0123456789'
26hexdigits = digits + 'abcdef' + 'ABCDEF'
27octdigits = '01234567'
28
29# Case conversion helpers
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000030_idmap = ''
31for i in range(256): _idmap = _idmap + chr(i)
Guido van Rossumc6360141990-10-13 19:23:40 +000032del i
33
Guido van Rossum710c3521994-08-17 13:16:11 +000034# Backward compatible names for exceptions
35index_error = ValueError
36atoi_error = ValueError
37atof_error = ValueError
38atol_error = ValueError
39
Guido van Rossumc6360141990-10-13 19:23:40 +000040# convert UPPER CASE letters to lower case
41def lower(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000042 """lower(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000043
Barry Warsaw226ae6c1999-10-12 19:54:53 +000044 Return a copy of the string s converted to lowercase.
Guido van Rossum20032041997-12-29 19:26:28 +000045
Barry Warsaw226ae6c1999-10-12 19:54:53 +000046 """
47 return s.lower()
Guido van Rossumc6360141990-10-13 19:23:40 +000048
49# Convert lower case letters to UPPER CASE
50def upper(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000051 """upper(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000052
Barry Warsaw226ae6c1999-10-12 19:54:53 +000053 Return a copy of the string s converted to uppercase.
Guido van Rossum20032041997-12-29 19:26:28 +000054
Barry Warsaw226ae6c1999-10-12 19:54:53 +000055 """
56 return s.upper()
Guido van Rossumc6360141990-10-13 19:23:40 +000057
58# Swap lower case letters and UPPER CASE
59def swapcase(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000060 """swapcase(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000061
Barry Warsaw226ae6c1999-10-12 19:54:53 +000062 Return a copy of the string s with upper case characters
63 converted to lowercase and vice versa.
Guido van Rossum20032041997-12-29 19:26:28 +000064
Barry Warsaw226ae6c1999-10-12 19:54:53 +000065 """
66 return s.swapcase()
Guido van Rossumc6360141990-10-13 19:23:40 +000067
68# Strip leading and trailing tabs and spaces
69def strip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000070 """strip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000071
Barry Warsaw226ae6c1999-10-12 19:54:53 +000072 Return a copy of the string s with leading and trailing
73 whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000074
Barry Warsaw226ae6c1999-10-12 19:54:53 +000075 """
76 return s.strip()
Guido van Rossumc6360141990-10-13 19:23:40 +000077
Guido van Rossum306a8a61996-08-08 18:40:59 +000078# Strip leading tabs and spaces
79def lstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000080 """lstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000081
Barry Warsaw226ae6c1999-10-12 19:54:53 +000082 Return a copy of the string s with leading whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000083
Barry Warsaw226ae6c1999-10-12 19:54:53 +000084 """
85 return s.lstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000086
87# Strip trailing tabs and spaces
88def rstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000089 """rstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000090
Barry Warsaw226ae6c1999-10-12 19:54:53 +000091 Return a copy of the string s with trailing whitespace
92 removed.
Guido van Rossum20032041997-12-29 19:26:28 +000093
Barry Warsaw226ae6c1999-10-12 19:54:53 +000094 """
95 return s.rstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000096
97
Guido van Rossumc6360141990-10-13 19:23:40 +000098# Split a string into a list of space/tab-separated words
99# NB: split(s) is NOT the same as splitfields(s, ' ')!
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000100def split(s, sep=None, maxsplit=-1):
Fred Drakee4f13661999-11-04 19:19:48 +0000101 """split(s [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000102
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000103 Return a list of the words in the string s, using sep as the
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000104 delimiter string. If maxsplit is given, splits into at most
Fred Drakee4f13661999-11-04 19:19:48 +0000105 maxsplit words. If sep is not specified, any whitespace string
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000106 is a separator.
Guido van Rossum20032041997-12-29 19:26:28 +0000107
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000108 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000109
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000110 """
111 return s.split(sep, maxsplit)
112splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000113
Guido van Rossum2ab19921995-06-22 18:58:00 +0000114# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000115def join(words, sep = ' '):
116 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000117
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000118 Return a string composed of the words in list, with
119 intervening occurences of sep. The default separator is a
120 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000121
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000122 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000123
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000124 """
125 return sep.join(words)
126joinfields = join
127
128# for a little bit of speed
129_apply = apply
Guido van Rossumfac38b71991-04-07 13:42:19 +0000130
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 """
138 return _apply(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 """
147 return _apply(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 """
158 return _apply(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 """
171 return _apply(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 """
184 return _apply(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 """
242 n = width - len(s)
243 if n <= 0: return s
244 return s + ' '*n
Guido van Rossumc6360141990-10-13 19:23:40 +0000245
246# Right-justify a string
247def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000248 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000249
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000250 Return a right-justified version of s, in a field of the
251 specified width, padded with spaces as needed. The string is
252 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000253
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000254 """
255 n = width - len(s)
256 if n <= 0: return s
257 return ' '*n + s
Guido van Rossumc6360141990-10-13 19:23:40 +0000258
259# Center a string
260def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000261 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000262
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000263 Return a center version of s, in a field of the specified
264 width. padded with spaces as needed. The string is never
265 truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000266
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000267 """
268 n = width - len(s)
269 if n <= 0: return s
270 half = n/2
271 if n%2 and width%2:
Fred Drake857c4c32000-02-10 16:21:11 +0000272 # This ensures that center(center(s, i), j) = center(s, j)
273 half = half+1
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000274 return ' '*half + s + ' '*(n-half)
Guido van Rossumc6360141990-10-13 19:23:40 +0000275
276# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
277# Decadent feature: the argument may be a string or a number
278# (Use of this is deprecated; it should be a string as with ljust c.s.)
279def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000280 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000281
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000282 Pad a numeric string x with zeros on the left, to fill a field
283 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000284
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000285 """
286 if type(x) == type(''): s = x
287 else: s = `x`
288 n = len(s)
289 if n >= width: return s
290 sign = ''
291 if s[0] in ('-', '+'):
Fred Drake857c4c32000-02-10 16:21:11 +0000292 sign, s = s[0], s[1:]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000293 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000294
295# Expand tabs in a string.
296# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000297def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000298 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000299
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000300 Return a copy of the string s with all tab characters replaced
301 by the appropriate number of spaces, depending on the current
302 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000303
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000304 """
305 res = line = ''
306 for c in s:
Fred Drake857c4c32000-02-10 16:21:11 +0000307 if c == '\t':
308 c = ' '*(tabsize - len(line) % tabsize)
309 line = line + c
310 if c == '\n':
311 res = res + line
312 line = ''
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000313 return res + line
Guido van Rossum2db91351992-10-18 17:09:59 +0000314
Guido van Rossum25395281996-05-28 23:08:45 +0000315# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000316def translate(s, table, deletions=""):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000317 """translate(s,table [,deletechars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000318
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000319 Return a copy of the string s, where all characters occurring
320 in the optional argument deletechars are removed, and the
321 remaining characters have been mapped through the given
322 translation table, which must be a string of length 256.
Guido van Rossum20032041997-12-29 19:26:28 +0000323
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000324 """
325 return s.translate(table, deletions)
Guido van Rossum2db91351992-10-18 17:09:59 +0000326
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000327# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
328def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000329 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000330
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000331 Return a copy of the string s with only its first character
332 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000333
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000334 """
335 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000336
337# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
338# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000339def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000340 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000341
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000342 Split the argument into words using split, capitalize each
343 word using capitalize, and join the capitalized words using
344 join. Note that this replaces runs of whitespace characters by
345 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000346
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000347 """
348 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000349
Guido van Rossumed7253c1996-07-23 18:12:39 +0000350# Construct a translation string
351_idmapL = None
352def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000353 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000354
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000355 Return a translation table (a string of 256 bytes long)
356 suitable for use in string.translate. The strings frm and to
357 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000358
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000359 """
360 if len(fromstr) != len(tostr):
Fred Drake857c4c32000-02-10 16:21:11 +0000361 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000362 global _idmapL
363 if not _idmapL:
Fred Drake857c4c32000-02-10 16:21:11 +0000364 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000365 L = _idmapL[:]
366 fromstr = map(ord, fromstr)
367 for i in range(len(fromstr)):
Fred Drake857c4c32000-02-10 16:21:11 +0000368 L[fromstr[i]] = tostr[i]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000369 return joinfields(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000370
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000371# Substring replacement (global)
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000372def replace(s, old, new, maxsplit=-1):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000373 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000374
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000375 Return a copy of string str with all occurrences of substring
376 old replaced by new. If the optional argument maxsplit is
377 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000378
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000379 """
380 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000381
382
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000383# XXX: transitional
384#
385# If string objects do not have methods, then we need to use the old string.py
386# library, which uses strop for many more things than just the few outlined
387# below.
388try:
389 ''.upper
390except AttributeError:
391 from stringold import *
392
Guido van Rossum2db91351992-10-18 17:09:59 +0000393# Try importing optional built-in module "strop" -- if it exists,
394# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000395# It also defines values for whitespace, lowercase and uppercase
396# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000397
398try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000399 from strop import maketrans, lowercase, uppercase, whitespace
400 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000401except ImportError:
Fred Drake857c4c32000-02-10 16:21:11 +0000402 pass # Use the original versions