blob: ebfe38313a7f6ac0a3b96ead28839024c43986dc [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# module 'string' -- A collection of string operations
2
Barry Warsaw226ae6c1999-10-12 19:54:53 +00003# Warning: most of the code you see here isn't normally used nowadays. With
4# Python 1.6, many of these functions are implemented as methods on the
5# standard string object. They used to be implemented by a built-in module
6# called strop, but strop is now obsolete itself.
Guido van Rossumc6360141990-10-13 19:23:40 +00007
Guido van Rossum20032041997-12-29 19:26:28 +00008"""Common string manipulations.
9
10Public module variables:
11
12whitespace -- a string containing all characters considered whitespace
13lowercase -- a string containing all characters considered lowercase letters
14uppercase -- a string containing all characters considered uppercase letters
15letters -- a string containing all characters considered letters
16digits -- a string containing all characters considered decimal digits
17hexdigits -- a string containing all characters considered hexadecimal digits
18octdigits -- a string containing all characters considered octal digits
19
20"""
Brett Cannon6071cc82008-05-08 19:52:45 +000021from warnings import warnpy3k
22warnpy3k("the stringold module has been removed in Python 3.0", stacklevel=2)
23del warnpy3k
Guido van Rossum20032041997-12-29 19:26:28 +000024
Guido van Rossumc6360141990-10-13 19:23:40 +000025# Some strings for ctype-style character classification
Guido van Rossum8e2ec561993-07-29 09:37:38 +000026whitespace = ' \t\n\r\v\f'
Guido van Rossumc6360141990-10-13 19:23:40 +000027lowercase = 'abcdefghijklmnopqrstuvwxyz'
28uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
29letters = lowercase + uppercase
30digits = '0123456789'
31hexdigits = digits + 'abcdef' + 'ABCDEF'
32octdigits = '01234567'
33
34# Case conversion helpers
Guido van Rossuma61ff7b1992-01-14 18:31:29 +000035_idmap = ''
36for i in range(256): _idmap = _idmap + chr(i)
Guido van Rossumc6360141990-10-13 19:23:40 +000037del i
38
Guido van Rossum710c3521994-08-17 13:16:11 +000039# Backward compatible names for exceptions
40index_error = ValueError
41atoi_error = ValueError
42atof_error = ValueError
43atol_error = ValueError
44
Guido van Rossumc6360141990-10-13 19:23:40 +000045# convert UPPER CASE letters to lower case
46def lower(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000047 """lower(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000048
Barry Warsaw226ae6c1999-10-12 19:54:53 +000049 Return a copy of the string s converted to lowercase.
Guido van Rossum20032041997-12-29 19:26:28 +000050
Barry Warsaw226ae6c1999-10-12 19:54:53 +000051 """
52 return s.lower()
Guido van Rossumc6360141990-10-13 19:23:40 +000053
54# Convert lower case letters to UPPER CASE
55def upper(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000056 """upper(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000057
Barry Warsaw226ae6c1999-10-12 19:54:53 +000058 Return a copy of the string s converted to uppercase.
Guido van Rossum20032041997-12-29 19:26:28 +000059
Barry Warsaw226ae6c1999-10-12 19:54:53 +000060 """
61 return s.upper()
Guido van Rossumc6360141990-10-13 19:23:40 +000062
63# Swap lower case letters and UPPER CASE
64def swapcase(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000065 """swapcase(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000066
Barry Warsaw226ae6c1999-10-12 19:54:53 +000067 Return a copy of the string s with upper case characters
68 converted to lowercase and vice versa.
Guido van Rossum20032041997-12-29 19:26:28 +000069
Barry Warsaw226ae6c1999-10-12 19:54:53 +000070 """
71 return s.swapcase()
Guido van Rossumc6360141990-10-13 19:23:40 +000072
73# Strip leading and trailing tabs and spaces
74def strip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000075 """strip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000076
Barry Warsaw226ae6c1999-10-12 19:54:53 +000077 Return a copy of the string s with leading and trailing
78 whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000079
Barry Warsaw226ae6c1999-10-12 19:54:53 +000080 """
81 return s.strip()
Guido van Rossumc6360141990-10-13 19:23:40 +000082
Guido van Rossum306a8a61996-08-08 18:40:59 +000083# Strip leading tabs and spaces
84def lstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000085 """lstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000086
Barry Warsaw226ae6c1999-10-12 19:54:53 +000087 Return a copy of the string s with leading whitespace removed.
Guido van Rossum20032041997-12-29 19:26:28 +000088
Barry Warsaw226ae6c1999-10-12 19:54:53 +000089 """
90 return s.lstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +000091
92# Strip trailing tabs and spaces
93def rstrip(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000094 """rstrip(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000095
Barry Warsaw226ae6c1999-10-12 19:54:53 +000096 Return a copy of the string s with trailing whitespace
97 removed.
Guido van Rossum20032041997-12-29 19:26:28 +000098
Barry Warsaw226ae6c1999-10-12 19:54:53 +000099 """
100 return s.rstrip()
Guido van Rossum306a8a61996-08-08 18:40:59 +0000101
102
Guido van Rossumc6360141990-10-13 19:23:40 +0000103# Split a string into a list of space/tab-separated words
Guido van Rossum306a8a61996-08-08 18:40:59 +0000104def split(s, sep=None, maxsplit=0):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000105 """split(str [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000106
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000107 Return a list of the words in the string s, using sep as the
108 delimiter string. If maxsplit is nonzero, splits into at most
109 maxsplit words If sep is not specified, any whitespace string
110 is a separator. Maxsplit defaults to 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000111
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000112 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000113
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000114 """
115 return s.split(sep, maxsplit)
116splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000117
Guido van Rossum2ab19921995-06-22 18:58:00 +0000118# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000119def join(words, sep = ' '):
120 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000121
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000122 Return a string composed of the words in list, with
Thomas Wouters7e474022000-07-16 12:04:32 +0000123 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000124 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000125
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000126 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000127
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000128 """
129 return sep.join(words)
130joinfields = join
131
132# for a little bit of speed
133_apply = apply
Guido van Rossumfac38b71991-04-07 13:42:19 +0000134
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 """
142 return _apply(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 """
151 return _apply(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 """
162 return _apply(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 """
175 return _apply(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 """
188 return _apply(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
194_StringType = type('')
Guido van Rossumd0753e21997-12-10 22:59:55 +0000195
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000196# Convert string to float
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000197def atof(s):
198 """atof(s) -> float
Guido van Rossum20032041997-12-29 19:26:28 +0000199
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000200 Return the floating point number represented by the string s.
Guido van Rossum20032041997-12-29 19:26:28 +0000201
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000202 """
203 if type(s) == _StringType:
Fred Drake13a2c272000-02-10 17:17:14 +0000204 return _float(s)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000205 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000206 raise TypeError('argument 1: expected string, %s found' %
207 type(s).__name__)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000208
Guido van Rossumc6360141990-10-13 19:23:40 +0000209# Convert string to integer
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000210def atoi(*args):
211 """atoi(s [,base]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000212
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000213 Return the integer represented by the string s in the given
214 base, which defaults to 10. The string s must consist of one
215 or more digits, possibly preceded by a sign. If base is 0, it
216 is chosen from the leading characters of s, 0 for octal, 0x or
217 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
218 accepted.
Guido van Rossum20032041997-12-29 19:26:28 +0000219
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000220 """
221 try:
Fred Drake13a2c272000-02-10 17:17:14 +0000222 s = args[0]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000223 except IndexError:
Fred Drake13a2c272000-02-10 17:17:14 +0000224 raise TypeError('function requires at least 1 argument: %d given' %
225 len(args))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000226 # Don't catch type error resulting from too many arguments to int(). The
227 # error message isn't compatible but the error type is, and this function
228 # is complicated enough already.
229 if type(s) == _StringType:
Fred Drake13a2c272000-02-10 17:17:14 +0000230 return _apply(_int, args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000231 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000232 raise TypeError('argument 1: expected string, %s found' %
233 type(s).__name__)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000234
Guido van Rossumc6360141990-10-13 19:23:40 +0000235
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000236# Convert string to long integer
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000237def atol(*args):
238 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000239
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000240 Return the long integer represented by the string s in the
241 given base, which defaults to 10. The string s must consist
242 of one or more digits, possibly preceded by a sign. If base
243 is 0, it is chosen from the leading characters of s, 0 for
244 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
245 0x or 0X is accepted. A trailing L or l is not accepted,
246 unless base is 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000247
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000248 """
249 try:
Fred Drake13a2c272000-02-10 17:17:14 +0000250 s = args[0]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000251 except IndexError:
Fred Drake13a2c272000-02-10 17:17:14 +0000252 raise TypeError('function requires at least 1 argument: %d given' %
253 len(args))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000254 # Don't catch type error resulting from too many arguments to long(). The
255 # error message isn't compatible but the error type is, and this function
256 # is complicated enough already.
257 if type(s) == _StringType:
Fred Drake13a2c272000-02-10 17:17:14 +0000258 return _apply(_long, args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000259 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000260 raise TypeError('argument 1: expected string, %s found' %
261 type(s).__name__)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000262
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000263
Guido van Rossumc6360141990-10-13 19:23:40 +0000264# Left-justify a string
265def ljust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000266 """ljust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000267
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000268 Return a left-justified version of s, in a field of the
269 specified width, padded with spaces as needed. The string is
270 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000271
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000272 """
273 n = width - len(s)
274 if n <= 0: return s
275 return s + ' '*n
Guido van Rossumc6360141990-10-13 19:23:40 +0000276
277# Right-justify a string
278def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000279 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000280
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000281 Return a right-justified version of s, in a field of the
282 specified width, padded with spaces as needed. The string is
283 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000284
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000285 """
286 n = width - len(s)
287 if n <= 0: return s
288 return ' '*n + s
Guido van Rossumc6360141990-10-13 19:23:40 +0000289
290# Center a string
291def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000292 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000293
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000294 Return a center version of s, in a field of the specified
295 width. padded with spaces as needed. The string is never
296 truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000297
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000298 """
299 n = width - len(s)
300 if n <= 0: return s
301 half = n/2
302 if n%2 and width%2:
Fred Drake13a2c272000-02-10 17:17:14 +0000303 # This ensures that center(center(s, i), j) = center(s, j)
304 half = half+1
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000305 return ' '*half + s + ' '*(n-half)
Guido van Rossumc6360141990-10-13 19:23:40 +0000306
307# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
308# Decadent feature: the argument may be a string or a number
309# (Use of this is deprecated; it should be a string as with ljust c.s.)
310def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000311 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000312
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000313 Pad a numeric string x with zeros on the left, to fill a field
314 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000315
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000316 """
317 if type(x) == type(''): s = x
Walter Dörwald70a6b492004-02-12 17:35:32 +0000318 else: s = repr(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000319 n = len(s)
320 if n >= width: return s
321 sign = ''
322 if s[0] in ('-', '+'):
Fred Drake13a2c272000-02-10 17:17:14 +0000323 sign, s = s[0], s[1:]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000324 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000325
326# Expand tabs in a string.
327# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000328def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000329 """expandtabs(s [,tabsize]) -> 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 all tab characters replaced
332 by the appropriate number of spaces, depending on the current
333 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000334
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000335 """
336 res = line = ''
337 for c in s:
Fred Drake13a2c272000-02-10 17:17:14 +0000338 if c == '\t':
339 c = ' '*(tabsize - len(line) % tabsize)
340 line = line + c
341 if c == '\n':
342 res = res + line
343 line = ''
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344 return res + line
Guido van Rossum2db91351992-10-18 17:09:59 +0000345
Guido van Rossum25395281996-05-28 23:08:45 +0000346# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000347def translate(s, table, deletions=""):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000348 """translate(s,table [,deletechars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000349
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000350 Return a copy of the string s, where all characters occurring
351 in the optional argument deletechars are removed, and the
352 remaining characters have been mapped through the given
353 translation table, which must be a string of length 256.
Guido van Rossum20032041997-12-29 19:26:28 +0000354
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000355 """
356 return s.translate(table, deletions)
Guido van Rossum2db91351992-10-18 17:09:59 +0000357
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000358# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
359def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000360 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000361
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000362 Return a copy of the string s with only its first character
363 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000364
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000365 """
366 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000367
368# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
Guido van Rossum34f17311996-08-20 20:25:41 +0000369def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000370 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000371
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000372 Split the argument into words using split, capitalize each
373 word using capitalize, and join the capitalized words using
374 join. Note that this replaces runs of whitespace characters by
375 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000376
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000377 """
378 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000379
Guido van Rossumed7253c1996-07-23 18:12:39 +0000380# Construct a translation string
381_idmapL = None
382def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000383 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000384
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000385 Return a translation table (a string of 256 bytes long)
386 suitable for use in string.translate. The strings frm and to
387 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000388
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000389 """
390 if len(fromstr) != len(tostr):
Fred Drake13a2c272000-02-10 17:17:14 +0000391 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000392 global _idmapL
393 if not _idmapL:
Georg Brandl74bbc792008-07-18 19:06:13 +0000394 _idmapL = list(_idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000395 L = _idmapL[:]
396 fromstr = map(ord, fromstr)
397 for i in range(len(fromstr)):
Fred Drake13a2c272000-02-10 17:17:14 +0000398 L[fromstr[i]] = tostr[i]
Eric S. Raymonde37340e2001-02-09 16:56:44 +0000399 return join(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000400
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000401# Substring replacement (global)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000402def replace(s, old, new, maxsplit=0):
403 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000404
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000405 Return a copy of string str with all occurrences of substring
406 old replaced by new. If the optional argument maxsplit is
407 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000408
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000409 """
410 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000411
412
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000413# XXX: transitional
414#
415# If string objects do not have methods, then we need to use the old string.py
416# library, which uses strop for many more things than just the few outlined
417# below.
418try:
419 ''.upper
420except AttributeError:
421 from stringold import *
422
Guido van Rossum2db91351992-10-18 17:09:59 +0000423# Try importing optional built-in module "strop" -- if it exists,
424# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000425# It also defines values for whitespace, lowercase and uppercase
426# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000427
428try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000429 from strop import maketrans, lowercase, uppercase, whitespace
430 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000431except ImportError:
Fred Drake13a2c272000-02-10 17:17:14 +0000432 pass # Use the original versions