blob: d7969dd69e76b5ea48dd3965951dc48128d96d63 [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 Rossum306a8a61996-08-08 18:40:59 +0000100def split(s, sep=None, maxsplit=0):
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
104 delimiter string. If maxsplit is nonzero, splits into at most
Fred Drakee4f13661999-11-04 19:19:48 +0000105 maxsplit words. If sep is not specified, any whitespace string
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000106 is a separator. Maxsplit defaults to 0.
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 """
199 if type(s) == _StringType:
200 return _float(s)
201 else:
202 raise TypeError('argument 1: expected string, %s found' %
203 type(s).__name__)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000204
Guido van Rossumc6360141990-10-13 19:23:40 +0000205# Convert string to integer
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000206def atoi(*args):
207 """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 """
217 try:
218 s = args[0]
219 except IndexError:
220 raise TypeError('function requires at least 1 argument: %d given' %
221 len(args))
222 # Don't catch type error resulting from too many arguments to int(). The
223 # error message isn't compatible but the error type is, and this function
224 # is complicated enough already.
225 if type(s) == _StringType:
226 return _apply(_int, args)
227 else:
228 raise TypeError('argument 1: expected string, %s found' %
229 type(s).__name__)
230
Guido van Rossumc6360141990-10-13 19:23:40 +0000231
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000232# Convert string to long integer
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000233def atol(*args):
234 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000235
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000236 Return the long integer represented by the string s in the
237 given base, which defaults to 10. The string s must consist
238 of one or more digits, possibly preceded by a sign. If base
239 is 0, it is chosen from the leading characters of s, 0 for
240 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
241 0x or 0X is accepted. A trailing L or l is not accepted,
242 unless base is 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000243
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000244 """
245 try:
246 s = args[0]
247 except IndexError:
248 raise TypeError('function requires at least 1 argument: %d given' %
249 len(args))
250 # Don't catch type error resulting from too many arguments to long(). The
251 # error message isn't compatible but the error type is, and this function
252 # is complicated enough already.
253 if type(s) == _StringType:
254 return _apply(_long, args)
255 else:
256 raise TypeError('argument 1: expected string, %s found' %
257 type(s).__name__)
258
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000259
Guido van Rossumc6360141990-10-13 19:23:40 +0000260# Left-justify a string
261def ljust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000262 """ljust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000263
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000264 Return a left-justified version of s, in a field of the
265 specified width, padded with spaces as needed. The string is
266 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000267
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000268 """
269 n = width - len(s)
270 if n <= 0: return s
271 return s + ' '*n
Guido van Rossumc6360141990-10-13 19:23:40 +0000272
273# Right-justify a string
274def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000275 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000276
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000277 Return a right-justified version of s, in a field of the
278 specified width, padded with spaces as needed. The string is
279 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000280
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000281 """
282 n = width - len(s)
283 if n <= 0: return s
284 return ' '*n + s
Guido van Rossumc6360141990-10-13 19:23:40 +0000285
286# Center a string
287def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000288 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000289
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000290 Return a center version of s, in a field of the specified
291 width. padded with spaces as needed. The string is never
292 truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000293
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000294 """
295 n = width - len(s)
296 if n <= 0: return s
297 half = n/2
298 if n%2 and width%2:
299 # This ensures that center(center(s, i), j) = center(s, j)
300 half = half+1
301 return ' '*half + s + ' '*(n-half)
Guido van Rossumc6360141990-10-13 19:23:40 +0000302
303# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
304# Decadent feature: the argument may be a string or a number
305# (Use of this is deprecated; it should be a string as with ljust c.s.)
306def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000307 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000308
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000309 Pad a numeric string x with zeros on the left, to fill a field
310 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000311
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000312 """
313 if type(x) == type(''): s = x
314 else: s = `x`
315 n = len(s)
316 if n >= width: return s
317 sign = ''
318 if s[0] in ('-', '+'):
319 sign, s = s[0], s[1:]
320 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000321
322# Expand tabs in a string.
323# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000324def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000325 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000326
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000327 Return a copy of the string s with all tab characters replaced
328 by the appropriate number of spaces, depending on the current
329 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000330
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000331 """
332 res = line = ''
333 for c in s:
334 if c == '\t':
335 c = ' '*(tabsize - len(line) % tabsize)
336 line = line + c
337 if c == '\n':
338 res = res + line
339 line = ''
340 return res + line
Guido van Rossum2db91351992-10-18 17:09:59 +0000341
Guido van Rossum25395281996-05-28 23:08:45 +0000342# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000343def translate(s, table, deletions=""):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344 """translate(s,table [,deletechars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000345
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000346 Return a copy of the string s, where all characters occurring
347 in the optional argument deletechars are removed, and the
348 remaining characters have been mapped through the given
349 translation table, which must be a string of length 256.
Guido van Rossum20032041997-12-29 19:26:28 +0000350
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000351 """
352 return s.translate(table, deletions)
Guido van Rossum2db91351992-10-18 17:09:59 +0000353
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000354# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
355def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000356 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000357
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000358 Return a copy of the string s with only its first character
359 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000360
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000361 """
362 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000363
364# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
365# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000366def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000367 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000368
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000369 Split the argument into words using split, capitalize each
370 word using capitalize, and join the capitalized words using
371 join. Note that this replaces runs of whitespace characters by
372 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000373
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000374 """
375 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000376
Guido van Rossumed7253c1996-07-23 18:12:39 +0000377# Construct a translation string
378_idmapL = None
379def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000380 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000381
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000382 Return a translation table (a string of 256 bytes long)
383 suitable for use in string.translate. The strings frm and to
384 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000385
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000386 """
387 if len(fromstr) != len(tostr):
388 raise ValueError, "maketrans arguments must have same length"
389 global _idmapL
390 if not _idmapL:
391 _idmapL = map(None, _idmap)
392 L = _idmapL[:]
393 fromstr = map(ord, fromstr)
394 for i in range(len(fromstr)):
395 L[fromstr[i]] = tostr[i]
396 return joinfields(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000397
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000398# Substring replacement (global)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000399def replace(s, old, new, maxsplit=0):
400 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000401
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000402 Return a copy of string str with all occurrences of substring
403 old replaced by new. If the optional argument maxsplit is
404 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000405
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000406 """
407 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000408
409
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000410# XXX: transitional
411#
412# If string objects do not have methods, then we need to use the old string.py
413# library, which uses strop for many more things than just the few outlined
414# below.
415try:
416 ''.upper
417except AttributeError:
418 from stringold import *
419
Guido van Rossum2db91351992-10-18 17:09:59 +0000420# Try importing optional built-in module "strop" -- if it exists,
421# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000422# It also defines values for whitespace, lowercase and uppercase
423# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000424
425try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000426 from strop import maketrans, lowercase, uppercase, whitespace
427 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000428except ImportError:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000429 pass # Use the original versions