blob: 8b6343a4ed600568914bdae15acab4029175c42d [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'
Fred Drake6b2320f2000-09-18 16:46:17 +000028punctuaction = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
29printable = digits + letters + punctuation + whitespace
Guido van Rossumc6360141990-10-13 19:23:40 +000030
31# Case conversion helpers
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000032_idmap = ''
33for i in range(256): _idmap = _idmap + chr(i)
Guido van Rossumc6360141990-10-13 19:23:40 +000034del i
35
Guido van Rossum710c3521994-08-17 13:16:11 +000036# Backward compatible names for exceptions
37index_error = ValueError
38atoi_error = ValueError
39atof_error = ValueError
40atol_error = ValueError
41
Guido van Rossumc6360141990-10-13 19:23:40 +000042# convert UPPER CASE letters to lower case
43def lower(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000044 """lower(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000045
Barry Warsaw226ae6c1999-10-12 19:54:53 +000046 Return a copy of the string s converted to lowercase.
Guido van Rossum20032041997-12-29 19:26:28 +000047
Barry Warsaw226ae6c1999-10-12 19:54:53 +000048 """
49 return s.lower()
Guido van Rossumc6360141990-10-13 19:23:40 +000050
51# Convert lower case letters to UPPER CASE
52def upper(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000053 """upper(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000054
Barry Warsaw226ae6c1999-10-12 19:54:53 +000055 Return a copy of the string s converted to uppercase.
Guido van Rossum20032041997-12-29 19:26:28 +000056
Barry Warsaw226ae6c1999-10-12 19:54:53 +000057 """
58 return s.upper()
Guido van Rossumc6360141990-10-13 19:23:40 +000059
60# Swap lower case letters and UPPER CASE
61def swapcase(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000062 """swapcase(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000063
Barry Warsaw226ae6c1999-10-12 19:54:53 +000064 Return a copy of the string s with upper case characters
65 converted to lowercase and vice versa.
Guido van Rossum20032041997-12-29 19:26:28 +000066
Barry Warsaw226ae6c1999-10-12 19:54:53 +000067 """
68 return s.swapcase()
Guido van Rossumc6360141990-10-13 19:23:40 +000069
70# Strip leading and trailing tabs and spaces
71def strip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000072 """strip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000073
Barry Warsaw226ae6c1999-10-12 19:54:53 +000074 Return a copy of the string s with leading and trailing
75 whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000076
Barry Warsaw226ae6c1999-10-12 19:54:53 +000077 """
78 return s.strip()
Guido van Rossumc6360141990-10-13 19:23:40 +000079
Guido van Rossum306a8a61996-08-08 18:40:59 +000080# Strip leading tabs and spaces
81def lstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000082 """lstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000083
Barry Warsaw226ae6c1999-10-12 19:54:53 +000084 Return a copy of the string s with leading whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000085
Barry Warsaw226ae6c1999-10-12 19:54:53 +000086 """
87 return s.lstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000088
89# Strip trailing tabs and spaces
90def rstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000091 """rstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000092
Barry Warsaw226ae6c1999-10-12 19:54:53 +000093 Return a copy of the string s with trailing whitespace
94 removed.
Guido van Rossum20032041997-12-29 19:26:28 +000095
Barry Warsaw226ae6c1999-10-12 19:54:53 +000096 """
97 return s.rstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000098
99
Guido van Rossumc6360141990-10-13 19:23:40 +0000100# Split a string into a list of space/tab-separated words
101# NB: split(s) is NOT the same as splitfields(s, ' ')!
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000102def split(s, sep=None, maxsplit=-1):
Fred Drakee4f13661999-11-04 19:19:48 +0000103 """split(s [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000104
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000105 Return a list of the words in the string s, using sep as the
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000106 delimiter string. If maxsplit is given, splits into at most
Fred Drakee4f13661999-11-04 19:19:48 +0000107 maxsplit words. If sep is not specified, any whitespace string
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000108 is a separator.
Guido van Rossum20032041997-12-29 19:26:28 +0000109
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000110 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000111
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000112 """
113 return s.split(sep, maxsplit)
114splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000115
Guido van Rossum2ab19921995-06-22 18:58:00 +0000116# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000117def join(words, sep = ' '):
118 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000119
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000120 Return a string composed of the words in list, with
Thomas Wouters7e474022000-07-16 12:04:32 +0000121 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000122 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000123
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000124 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000125
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000126 """
127 return sep.join(words)
128joinfields = join
129
Guido van Rossumd3166071993-05-24 14:16:22 +0000130# Find substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000131def index(s, *args):
132 """index(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000133
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000134 Like find but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000135
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000136 """
Fred Drake046d2722000-07-03 07:23:13 +0000137 return s.index(*args)
Guido van Rossumd3166071993-05-24 14:16:22 +0000138
Guido van Rossume65cce51993-11-08 15:05:21 +0000139# Find last substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000140def rindex(s, *args):
141 """rindex(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000142
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000143 Like rfind but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000144
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000145 """
Fred Drake046d2722000-07-03 07:23:13 +0000146 return s.rindex(*args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147
148# Count non-overlapping occurrences of substring
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000149def count(s, *args):
150 """count(s, sub[, start[,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000151
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000152 Return the number of occurrences of substring sub in string
153 s[start:end]. Optional arguments start and end are
154 interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000155
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000156 """
Fred Drake046d2722000-07-03 07:23:13 +0000157 return s.count(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000158
Guido van Rossumd3166071993-05-24 14:16:22 +0000159# Find substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000160def find(s, *args):
161 """find(s, sub [,start [,end]]) -> in
Guido van Rossum20032041997-12-29 19:26:28 +0000162
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000163 Return the lowest index in s where substring sub is found,
164 such that sub is contained within s[start,end]. Optional
165 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000166
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000167 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000168
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000169 """
Fred Drake046d2722000-07-03 07:23:13 +0000170 return s.find(*args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000171
Guido van Rossume65cce51993-11-08 15:05:21 +0000172# Find last substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000173def rfind(s, *args):
174 """rfind(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000175
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000176 Return the highest index in s where substring sub is found,
177 such that sub is contained within s[start,end]. Optional
178 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000179
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000180 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000181
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000182 """
Fred Drake046d2722000-07-03 07:23:13 +0000183 return s.rfind(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000184
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000185# for a bit of speed
186_float = float
187_int = int
188_long = long
189_StringType = type('')
Guido van Rossumd0753e21997-12-10 22:59:55 +0000190
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000191# Convert string to float
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000192def atof(s):
193 """atof(s) -> float
Guido van Rossum20032041997-12-29 19:26:28 +0000194
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000195 Return the floating point number represented by the string s.
Guido van Rossum20032041997-12-29 19:26:28 +0000196
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000197 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000198 return _float(s)
199
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000200
Guido van Rossumc6360141990-10-13 19:23:40 +0000201# Convert string to integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000202def atoi(s , base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000203 """atoi(s [,base]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000204
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000205 Return the integer represented by the string s in the given
206 base, which defaults to 10. The string s must consist of one
207 or more digits, possibly preceded by a sign. If base is 0, it
208 is chosen from the leading characters of s, 0 for octal, 0x or
209 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
210 accepted.
Guido van Rossum20032041997-12-29 19:26:28 +0000211
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000212 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000213 return _int(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000214
Guido van Rossumc6360141990-10-13 19:23:40 +0000215
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000216# Convert string to long integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000217def atol(s, base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000218 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000219
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000220 Return the long integer represented by the string s in the
221 given base, which defaults to 10. The string s must consist
222 of one or more digits, possibly preceded by a sign. If base
223 is 0, it is chosen from the leading characters of s, 0 for
224 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
225 0x or 0X is accepted. A trailing L or l is not accepted,
226 unless base is 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000227
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000228 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000229 return _long(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000230
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000231
Guido van Rossumc6360141990-10-13 19:23:40 +0000232# Left-justify a string
233def ljust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000234 """ljust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000235
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000236 Return a left-justified version of s, in a field of the
237 specified width, padded with spaces as needed. The string is
238 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000239
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000240 """
Fred Drake046d2722000-07-03 07:23:13 +0000241 return s.ljust(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000242
243# Right-justify a string
244def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000245 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000246
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000247 Return a right-justified version of s, in a field of the
248 specified width, padded with spaces as needed. The string is
249 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000250
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000251 """
Fred Drake046d2722000-07-03 07:23:13 +0000252 return s.rjust(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000253
254# Center a string
255def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000256 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000257
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000258 Return a center version of s, in a field of the specified
259 width. padded with spaces as needed. The string is never
260 truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000261
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000262 """
Fred Drake046d2722000-07-03 07:23:13 +0000263 return s.center(width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000264
265# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
266# Decadent feature: the argument may be a string or a number
267# (Use of this is deprecated; it should be a string as with ljust c.s.)
268def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000269 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000270
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000271 Pad a numeric string x with zeros on the left, to fill a field
272 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000273
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000274 """
275 if type(x) == type(''): s = x
276 else: s = `x`
277 n = len(s)
278 if n >= width: return s
279 sign = ''
280 if s[0] in ('-', '+'):
Fred Drake857c4c32000-02-10 16:21:11 +0000281 sign, s = s[0], s[1:]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000282 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000283
284# Expand tabs in a string.
285# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000286def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000287 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000288
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000289 Return a copy of the string s with all tab characters replaced
290 by the appropriate number of spaces, depending on the current
291 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000292
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000293 """
Fred Drake046d2722000-07-03 07:23:13 +0000294 return s.expandtabs(tabsize)
Guido van Rossum2db91351992-10-18 17:09:59 +0000295
Guido van Rossum25395281996-05-28 23:08:45 +0000296# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000297def translate(s, table, deletions=""):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000298 """translate(s,table [,deletechars]) -> 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, where all characters occurring
301 in the optional argument deletechars are removed, and the
302 remaining characters have been mapped through the given
303 translation table, which must be a string of length 256.
Guido van Rossum20032041997-12-29 19:26:28 +0000304
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000305 """
306 return s.translate(table, deletions)
Guido van Rossum2db91351992-10-18 17:09:59 +0000307
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000308# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
309def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000310 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000311
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000312 Return a copy of the string s with only its first character
313 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000314
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000315 """
316 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000317
318# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
319# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000320def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000321 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000322
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000323 Split the argument into words using split, capitalize each
324 word using capitalize, and join the capitalized words using
325 join. Note that this replaces runs of whitespace characters by
326 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000327
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000328 """
329 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000330
Guido van Rossumed7253c1996-07-23 18:12:39 +0000331# Construct a translation string
332_idmapL = None
333def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000334 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000335
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000336 Return a translation table (a string of 256 bytes long)
337 suitable for use in string.translate. The strings frm and to
338 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000339
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000340 """
341 if len(fromstr) != len(tostr):
Fred Drake857c4c32000-02-10 16:21:11 +0000342 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000343 global _idmapL
344 if not _idmapL:
Fred Drake857c4c32000-02-10 16:21:11 +0000345 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000346 L = _idmapL[:]
347 fromstr = map(ord, fromstr)
348 for i in range(len(fromstr)):
Fred Drake857c4c32000-02-10 16:21:11 +0000349 L[fromstr[i]] = tostr[i]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000350 return joinfields(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000351
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000352# Substring replacement (global)
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000353def replace(s, old, new, maxsplit=-1):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000354 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000355
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000356 Return a copy of string str with all occurrences of substring
357 old replaced by new. If the optional argument maxsplit is
358 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000359
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000360 """
361 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000362
363
Guido van Rossum2db91351992-10-18 17:09:59 +0000364# Try importing optional built-in module "strop" -- if it exists,
365# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000366# It also defines values for whitespace, lowercase and uppercase
367# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000368
369try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000370 from strop import maketrans, lowercase, uppercase, whitespace
371 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000372except ImportError:
Fred Drake857c4c32000-02-10 16:21:11 +0000373 pass # Use the original versions