blob: 8388b07a06ce302fc65bb3cfbb85d67abb9bc182 [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"""
21
Guido van Rossumc6360141990-10-13 19:23:40 +000022# Some strings for ctype-style character classification
Guido van Rossum8e2ec561993-07-29 09:37:38 +000023whitespace = ' \t\n\r\v\f'
Guido van Rossumc6360141990-10-13 19:23:40 +000024lowercase = 'abcdefghijklmnopqrstuvwxyz'
25uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
26letters = lowercase + uppercase
27digits = '0123456789'
28hexdigits = digits + 'abcdef' + 'ABCDEF'
29octdigits = '01234567'
30
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
Guido van Rossum306a8a61996-08-08 18:40:59 +0000101def split(s, sep=None, maxsplit=0):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000102 """split(str [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000103
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000104 Return a list of the words in the string s, using sep as the
105 delimiter string. If maxsplit is nonzero, splits into at most
106 maxsplit words If sep is not specified, any whitespace string
107 is a separator. Maxsplit defaults to 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000108
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000109 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000110
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000111 """
112 return s.split(sep, maxsplit)
113splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000114
Guido van Rossum2ab19921995-06-22 18:58:00 +0000115# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000116def join(words, sep = ' '):
117 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000118
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000119 Return a string composed of the words in list, with
Thomas Wouters7e474022000-07-16 12:04:32 +0000120 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000121 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000122
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000123 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000124
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000125 """
126 return sep.join(words)
127joinfields = join
128
Guido van Rossumd3166071993-05-24 14:16:22 +0000129# Find substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000130def index(s, *args):
131 """index(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000132
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000133 Like find but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000134
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000135 """
Neal Norwitz7c307242006-03-17 08:28:24 +0000136 return s.index(*args)
Guido van Rossumd3166071993-05-24 14:16:22 +0000137
Guido van Rossume65cce51993-11-08 15:05:21 +0000138# Find last substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000139def rindex(s, *args):
140 """rindex(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000141
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000142 Like rfind but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000143
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000144 """
Neal Norwitz7c307242006-03-17 08:28:24 +0000145 return s.rindex(*args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146
147# Count non-overlapping occurrences of substring
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000148def count(s, *args):
149 """count(s, sub[, start[,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000150
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000151 Return the number of occurrences of substring sub in string
152 s[start:end]. Optional arguments start and end are
153 interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000154
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000155 """
Neal Norwitz7c307242006-03-17 08:28:24 +0000156 return s.count(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000157
Guido van Rossumd3166071993-05-24 14:16:22 +0000158# Find substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000159def find(s, *args):
160 """find(s, sub [,start [,end]]) -> in
Guido van Rossum20032041997-12-29 19:26:28 +0000161
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000162 Return the lowest index in s where substring sub is found,
163 such that sub is contained within s[start,end]. Optional
164 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000165
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000166 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000167
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000168 """
Neal Norwitz7c307242006-03-17 08:28:24 +0000169 return s.find(*args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000170
Guido van Rossume65cce51993-11-08 15:05:21 +0000171# Find last substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000172def rfind(s, *args):
173 """rfind(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000174
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000175 Return the highest index in s where substring sub is found,
176 such that sub is contained within s[start,end]. Optional
177 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000178
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000179 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000180
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000181 """
Neal Norwitz7c307242006-03-17 08:28:24 +0000182 return s.rfind(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000183
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000184# for a bit of speed
185_float = float
186_int = int
Guido van Rossume2a383d2007-01-15 16:59:06 +0000187_long = int
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000188_StringType = type('')
Guido van Rossumd0753e21997-12-10 22:59:55 +0000189
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000190# Convert string to float
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000191def atof(s):
192 """atof(s) -> float
Guido van Rossum20032041997-12-29 19:26:28 +0000193
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000194 Return the floating point number represented by the string s.
Guido van Rossum20032041997-12-29 19:26:28 +0000195
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000196 """
197 if type(s) == _StringType:
Fred Drake13a2c272000-02-10 17:17:14 +0000198 return _float(s)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000199 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000200 raise TypeError('argument 1: expected string, %s found' %
201 type(s).__name__)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000202
Guido van Rossumc6360141990-10-13 19:23:40 +0000203# Convert string to integer
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000204def atoi(*args):
205 """atoi(s [,base]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000206
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000207 Return the integer represented by the string s in the given
208 base, which defaults to 10. The string s must consist of one
209 or more digits, possibly preceded by a sign. If base is 0, it
210 is chosen from the leading characters of s, 0 for octal, 0x or
211 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
212 accepted.
Guido van Rossum20032041997-12-29 19:26:28 +0000213
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000214 """
215 try:
Fred Drake13a2c272000-02-10 17:17:14 +0000216 s = args[0]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000217 except IndexError:
Fred Drake13a2c272000-02-10 17:17:14 +0000218 raise TypeError('function requires at least 1 argument: %d given' %
219 len(args))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000220 # Don't catch type error resulting from too many arguments to int(). The
221 # error message isn't compatible but the error type is, and this function
222 # is complicated enough already.
223 if type(s) == _StringType:
Neal Norwitz7c307242006-03-17 08:28:24 +0000224 return _int(*args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000225 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000226 raise TypeError('argument 1: expected string, %s found' %
227 type(s).__name__)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000228
Guido van Rossumc6360141990-10-13 19:23:40 +0000229
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000230# Convert string to long integer
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000231def atol(*args):
232 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000233
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000234 Return the long integer represented by the string s in the
235 given base, which defaults to 10. The string s must consist
236 of one or more digits, possibly preceded by a sign. If base
237 is 0, it is chosen from the leading characters of s, 0 for
238 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
239 0x or 0X is accepted. A trailing L or l is not accepted,
240 unless base is 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000241
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000242 """
243 try:
Fred Drake13a2c272000-02-10 17:17:14 +0000244 s = args[0]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000245 except IndexError:
Fred Drake13a2c272000-02-10 17:17:14 +0000246 raise TypeError('function requires at least 1 argument: %d given' %
247 len(args))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000248 # Don't catch type error resulting from too many arguments to long(). The
249 # error message isn't compatible but the error type is, and this function
250 # is complicated enough already.
251 if type(s) == _StringType:
Neal Norwitz7c307242006-03-17 08:28:24 +0000252 return _long(*args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000253 else:
Fred Drake13a2c272000-02-10 17:17:14 +0000254 raise TypeError('argument 1: expected string, %s found' %
255 type(s).__name__)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000256
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000257
Guido van Rossumc6360141990-10-13 19:23:40 +0000258# Left-justify a string
259def ljust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000260 """ljust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000261
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000262 Return a left-justified version of s, in a field of the
263 specified width, padded with spaces as needed. The string is
264 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000265
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000266 """
267 n = width - len(s)
268 if n <= 0: return s
269 return s + ' '*n
Guido van Rossumc6360141990-10-13 19:23:40 +0000270
271# Right-justify a string
272def rjust(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000273 """rjust(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000274
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000275 Return a right-justified version of s, in a field of the
276 specified width, padded with spaces as needed. The string is
277 never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000278
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000279 """
280 n = width - len(s)
281 if n <= 0: return s
282 return ' '*n + s
Guido van Rossumc6360141990-10-13 19:23:40 +0000283
284# Center a string
285def center(s, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000286 """center(s, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000287
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000288 Return a center version of s, in a field of the specified
289 width. padded with spaces as needed. The string is never
290 truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000291
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000292 """
293 n = width - len(s)
294 if n <= 0: return s
295 half = n/2
296 if n%2 and width%2:
Fred Drake13a2c272000-02-10 17:17:14 +0000297 # This ensures that center(center(s, i), j) = center(s, j)
298 half = half+1
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000299 return ' '*half + s + ' '*(n-half)
Guido van Rossumc6360141990-10-13 19:23:40 +0000300
301# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
302# Decadent feature: the argument may be a string or a number
303# (Use of this is deprecated; it should be a string as with ljust c.s.)
304def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000305 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000306
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000307 Pad a numeric string x with zeros on the left, to fill a field
308 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000309
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000310 """
311 if type(x) == type(''): s = x
Walter Dörwald70a6b492004-02-12 17:35:32 +0000312 else: s = repr(x)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000313 n = len(s)
314 if n >= width: return s
315 sign = ''
316 if s[0] in ('-', '+'):
Fred Drake13a2c272000-02-10 17:17:14 +0000317 sign, s = s[0], s[1:]
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000318 return sign + '0'*(width-n) + s
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000319
320# Expand tabs in a string.
321# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000322def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000323 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000324
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000325 Return a copy of the string s with all tab characters replaced
326 by the appropriate number of spaces, depending on the current
327 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000328
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000329 """
330 res = line = ''
331 for c in s:
Fred Drake13a2c272000-02-10 17:17:14 +0000332 if c == '\t':
333 c = ' '*(tabsize - len(line) % tabsize)
334 line = line + c
335 if c == '\n':
336 res = res + line
337 line = ''
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000338 return res + line
Guido van Rossum2db91351992-10-18 17:09:59 +0000339
Guido van Rossum25395281996-05-28 23:08:45 +0000340# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000341def translate(s, table, deletions=""):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000342 """translate(s,table [,deletechars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000343
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344 Return a copy of the string s, where all characters occurring
345 in the optional argument deletechars are removed, and the
346 remaining characters have been mapped through the given
347 translation table, which must be a string of length 256.
Guido van Rossum20032041997-12-29 19:26:28 +0000348
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000349 """
350 return s.translate(table, deletions)
Guido van Rossum2db91351992-10-18 17:09:59 +0000351
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000352# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
353def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000354 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000355
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000356 Return a copy of the string s with only its first character
357 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000358
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000359 """
360 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000361
362# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
Guido van Rossum34f17311996-08-20 20:25:41 +0000363def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000364 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000365
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000366 Split the argument into words using split, capitalize each
367 word using capitalize, and join the capitalized words using
368 join. Note that this replaces runs of whitespace characters by
369 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000370
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000371 """
372 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000373
Guido van Rossumed7253c1996-07-23 18:12:39 +0000374# Construct a translation string
375_idmapL = None
376def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000377 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000378
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000379 Return a translation table (a string of 256 bytes long)
380 suitable for use in string.translate. The strings frm and to
381 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000382
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000383 """
384 if len(fromstr) != len(tostr):
Fred Drake13a2c272000-02-10 17:17:14 +0000385 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000386 global _idmapL
387 if not _idmapL:
Fred Drake13a2c272000-02-10 17:17:14 +0000388 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000389 L = _idmapL[:]
390 fromstr = map(ord, fromstr)
391 for i in range(len(fromstr)):
Fred Drake13a2c272000-02-10 17:17:14 +0000392 L[fromstr[i]] = tostr[i]
Eric S. Raymonde37340e2001-02-09 16:56:44 +0000393 return join(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000394
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000395# Substring replacement (global)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000396def replace(s, old, new, maxsplit=0):
397 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000398
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000399 Return a copy of string str with all occurrences of substring
400 old replaced by new. If the optional argument maxsplit is
401 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000402
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000403 """
404 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000405
406
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000407# XXX: transitional
408#
409# If string objects do not have methods, then we need to use the old string.py
410# library, which uses strop for many more things than just the few outlined
411# below.
412try:
413 ''.upper
414except AttributeError:
415 from stringold import *
416
Guido van Rossum2db91351992-10-18 17:09:59 +0000417# Try importing optional built-in module "strop" -- if it exists,
418# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000419# It also defines values for whitespace, lowercase and uppercase
420# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000421
422try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000423 from strop import maketrans, lowercase, uppercase, whitespace
424 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000425except ImportError:
Fred Drake13a2c272000-02-10 17:17:14 +0000426 pass # Use the original versions