blob: 6570fef3c20cc7f3e8904016d877d01970c7b2c0 [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
Thomas Wouters7e474022000-07-16 12:04:32 +0000119 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000120 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
Guido van Rossumd3166071993-05-24 14:16:22 +0000128# Find substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000129def index(s, *args):
130 """index(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000131
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000132 Like find but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000133
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000134 """
Fred Drake046d2722000-07-03 07:23:13 +0000135 return s.index(*args)
Guido van Rossumd3166071993-05-24 14:16:22 +0000136
Guido van Rossume65cce51993-11-08 15:05:21 +0000137# Find last substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000138def rindex(s, *args):
139 """rindex(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000140
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000141 Like rfind but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000142
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000143 """
Fred Drake046d2722000-07-03 07:23:13 +0000144 return s.rindex(*args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145
146# Count non-overlapping occurrences of substring
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000147def count(s, *args):
148 """count(s, sub[, start[,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000149
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000150 Return the number of occurrences of substring sub in string
151 s[start:end]. Optional arguments start and end are
152 interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000153
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000154 """
Fred Drake046d2722000-07-03 07:23:13 +0000155 return s.count(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000156
Guido van Rossumd3166071993-05-24 14:16:22 +0000157# Find substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000158def find(s, *args):
159 """find(s, sub [,start [,end]]) -> in
Guido van Rossum20032041997-12-29 19:26:28 +0000160
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000161 Return the lowest index in s where substring sub is found,
162 such that sub is contained within s[start,end]. Optional
163 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000164
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000165 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000166
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000167 """
Fred Drake046d2722000-07-03 07:23:13 +0000168 return s.find(*args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000169
Guido van Rossume65cce51993-11-08 15:05:21 +0000170# Find last substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000171def rfind(s, *args):
172 """rfind(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000173
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000174 Return the highest index in s where substring sub is found,
175 such that sub is contained within s[start,end]. Optional
176 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000177
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000179
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000180 """
Fred Drake046d2722000-07-03 07:23:13 +0000181 return s.rfind(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000182
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000183# for a bit of speed
184_float = float
185_int = int
186_long = long
187_StringType = type('')
Guido van Rossumd0753e21997-12-10 22:59:55 +0000188
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000189# Convert string to float
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000190def atof(s):
191 """atof(s) -> float
Guido van Rossum20032041997-12-29 19:26:28 +0000192
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000193 Return the floating point number represented by the string s.
Guido van Rossum20032041997-12-29 19:26:28 +0000194
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000195 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000196 return _float(s)
197
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000198
Guido van Rossumc6360141990-10-13 19:23:40 +0000199# Convert string to integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000200def atoi(s , base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000201 """atoi(s [,base]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000202
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000203 Return the integer represented by the string s in the given
204 base, which defaults to 10. The string s must consist of one
205 or more digits, possibly preceded by a sign. If base is 0, it
206 is chosen from the leading characters of s, 0 for octal, 0x or
207 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
208 accepted.
Guido van Rossum20032041997-12-29 19:26:28 +0000209
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000210 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000211 return _int(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000212
Guido van Rossumc6360141990-10-13 19:23:40 +0000213
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000214# Convert string to long integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000215def atol(s, base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000216 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000217
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000218 Return the long integer represented by the string s in the
219 given base, which defaults to 10. The string s must consist
220 of one or more digits, possibly preceded by a sign. If base
221 is 0, it is chosen from the leading characters of s, 0 for
222 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
223 0x or 0X is accepted. A trailing L or l is not accepted,
224 unless base is 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000225
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000226 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000227 return _long(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000228
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000229
Guido van Rossumc6360141990-10-13 19:23:40 +0000230# Left-justify a string
231def ljust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000232 """ljust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000233
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000234 Return a left-justified version of s, in a field of the
235 specified width, padded with spaces as needed. The string is
236 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000237
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000238 """
Fred Drake046d2722000-07-03 07:23:13 +0000239 return s.ljust(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000240
241# Right-justify a string
242def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000243 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000244
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000245 Return a right-justified version of s, in a field of the
246 specified width, padded with spaces as needed. The string is
247 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000248
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000249 """
Fred Drake046d2722000-07-03 07:23:13 +0000250 return s.rjust(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000251
252# Center a string
253def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000254 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000255
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000256 Return a center version of s, in a field of the specified
257 width. padded with spaces as needed. The string is never
258 truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000259
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000260 """
Fred Drake046d2722000-07-03 07:23:13 +0000261 return s.center(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000262
263# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
264# Decadent feature: the argument may be a string or a number
265# (Use of this is deprecated; it should be a string as with ljust c.s.)
266def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000267 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000268
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000269 Pad a numeric string x with zeros on the left, to fill a field
270 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000271
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000272 """
273 if type(x) == type(''): s = x
274 else: s = `x`
275 n = len(s)
276 if n >= width: return s
277 sign = ''
278 if s[0] in ('-', '+'):
Fred Drake857c4c32000-02-10 16:21:11 +0000279 sign, s = s[0], s[1:]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000280 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000281
282# Expand tabs in a string.
283# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000284def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000285 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000286
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000287 Return a copy of the string s with all tab characters replaced
288 by the appropriate number of spaces, depending on the current
289 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000290
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000291 """
Fred Drake046d2722000-07-03 07:23:13 +0000292 return s.expandtabs(tabsize)
Guido van Rossum2db91351992-10-18 17:09:59 +0000293
Guido van Rossum25395281996-05-28 23:08:45 +0000294# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000295def translate(s, table, deletions=""):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000296 """translate(s,table [,deletechars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000297
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000298 Return a copy of the string s, where all characters occurring
299 in the optional argument deletechars are removed, and the
300 remaining characters have been mapped through the given
301 translation table, which must be a string of length 256.
Guido van Rossum20032041997-12-29 19:26:28 +0000302
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000303 """
304 return s.translate(table, deletions)
Guido van Rossum2db91351992-10-18 17:09:59 +0000305
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000306# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
307def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000308 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000309
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000310 Return a copy of the string s with only its first character
311 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000312
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000313 """
314 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000315
316# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
317# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000318def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000319 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000320
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000321 Split the argument into words using split, capitalize each
322 word using capitalize, and join the capitalized words using
323 join. Note that this replaces runs of whitespace characters by
324 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000325
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000326 """
327 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000328
Guido van Rossumed7253c1996-07-23 18:12:39 +0000329# Construct a translation string
330_idmapL = None
331def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000332 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000333
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000334 Return a translation table (a string of 256 bytes long)
335 suitable for use in string.translate. The strings frm and to
336 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000337
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000338 """
339 if len(fromstr) != len(tostr):
Fred Drake857c4c32000-02-10 16:21:11 +0000340 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000341 global _idmapL
342 if not _idmapL:
Fred Drake857c4c32000-02-10 16:21:11 +0000343 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344 L = _idmapL[:]
345 fromstr = map(ord, fromstr)
346 for i in range(len(fromstr)):
Fred Drake857c4c32000-02-10 16:21:11 +0000347 L[fromstr[i]] = tostr[i]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000348 return joinfields(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000349
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000350# Substring replacement (global)
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000351def replace(s, old, new, maxsplit=-1):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000352 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000353
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000354 Return a copy of string str with all occurrences of substring
355 old replaced by new. If the optional argument maxsplit is
356 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000357
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000358 """
359 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000360
361
Guido van Rossum2db91351992-10-18 17:09:59 +0000362# Try importing optional built-in module "strop" -- if it exists,
363# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000364# It also defines values for whitespace, lowercase and uppercase
365# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000366
367try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000368 from strop import maketrans, lowercase, uppercase, whitespace
369 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000370except ImportError:
Fred Drake857c4c32000-02-10 16:21:11 +0000371 pass # Use the original versions