blob: bc10c20337a57704583d6ae9417ef4baad2a1cb2 [file] [log] [blame]
Skip Montanaro0b874442003-10-03 14:05:26 +00001"""A collection of string operations (most are no longer used).
Guido van Rossumc6360141990-10-13 19:23:40 +00002
Skip Montanaro0b874442003-10-03 14:05:26 +00003Warning: most of the code you see here isn't normally used nowadays.
4Beginning with Python 1.6, many of these functions are implemented as
5methods on the standard string object. They used to be implemented by
6a built-in module called 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
Fred Drakefd64c592000-09-18 19:38:11 +000017punctuation -- a string containing all characters considered punctuation
18printable -- a string containing all characters considered printable
Guido van Rossum20032041997-12-29 19:26:28 +000019
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
Fred Drake960fdf92001-07-20 18:38:26 +000027ascii_lowercase = lowercase
28ascii_uppercase = uppercase
29ascii_letters = ascii_lowercase + ascii_uppercase
Guido van Rossumc6360141990-10-13 19:23:40 +000030digits = '0123456789'
31hexdigits = digits + 'abcdef' + 'ABCDEF'
32octdigits = '01234567'
Tim Peters495ad3c2001-01-15 01:36:40 +000033punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
Fred Drake6b2320f2000-09-18 16:46:17 +000034printable = digits + letters + punctuation + whitespace
Guido van Rossumc6360141990-10-13 19:23:40 +000035
36# Case conversion helpers
Martin v. Löwis5357c652002-10-14 20:03:40 +000037# Use str to convert Unicode literal in case of -U
38l = map(chr, xrange(256))
39_idmap = str('').join(l)
40del l
Guido van Rossumc6360141990-10-13 19:23:40 +000041
Guido van Rossum710c3521994-08-17 13:16:11 +000042# Backward compatible names for exceptions
43index_error = ValueError
44atoi_error = ValueError
45atof_error = ValueError
46atol_error = ValueError
47
Guido van Rossumc6360141990-10-13 19:23:40 +000048# convert UPPER CASE letters to lower case
49def lower(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000050 """lower(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000051
Barry Warsaw226ae6c1999-10-12 19:54:53 +000052 Return a copy of the string s converted to lowercase.
Guido van Rossum20032041997-12-29 19:26:28 +000053
Barry Warsaw226ae6c1999-10-12 19:54:53 +000054 """
55 return s.lower()
Guido van Rossumc6360141990-10-13 19:23:40 +000056
57# Convert lower case letters to UPPER CASE
58def upper(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000059 """upper(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000060
Barry Warsaw226ae6c1999-10-12 19:54:53 +000061 Return a copy of the string s converted to uppercase.
Guido van Rossum20032041997-12-29 19:26:28 +000062
Barry Warsaw226ae6c1999-10-12 19:54:53 +000063 """
64 return s.upper()
Guido van Rossumc6360141990-10-13 19:23:40 +000065
66# Swap lower case letters and UPPER CASE
67def swapcase(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +000068 """swapcase(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000069
Barry Warsaw226ae6c1999-10-12 19:54:53 +000070 Return a copy of the string s with upper case characters
71 converted to lowercase and vice versa.
Guido van Rossum20032041997-12-29 19:26:28 +000072
Barry Warsaw226ae6c1999-10-12 19:54:53 +000073 """
74 return s.swapcase()
Guido van Rossumc6360141990-10-13 19:23:40 +000075
76# Strip leading and trailing tabs and spaces
Martin v. Löwis1f046102002-11-08 12:09:59 +000077def strip(s, chars=None):
Neal Norwitza4864a22002-11-14 03:31:32 +000078 """strip(s [,chars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000079
Barry Warsaw226ae6c1999-10-12 19:54:53 +000080 Return a copy of the string s with leading and trailing
81 whitespace removed.
Neal Norwitzffe33b72003-04-10 22:35:32 +000082 If chars is given and not None, remove characters in chars instead.
Neal Norwitza4864a22002-11-14 03:31:32 +000083 If chars is unicode, S will be converted to unicode before stripping.
Guido van Rossum20032041997-12-29 19:26:28 +000084
Barry Warsaw226ae6c1999-10-12 19:54:53 +000085 """
Martin v. Löwis1f046102002-11-08 12:09:59 +000086 return s.strip(chars)
Guido van Rossumc6360141990-10-13 19:23:40 +000087
Guido van Rossum306a8a61996-08-08 18:40:59 +000088# Strip leading tabs and spaces
Neal Norwitzffe33b72003-04-10 22:35:32 +000089def lstrip(s, chars=None):
90 """lstrip(s [,chars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +000091
Barry Warsaw226ae6c1999-10-12 19:54:53 +000092 Return a copy of the string s with leading whitespace removed.
Neal Norwitzffe33b72003-04-10 22:35:32 +000093 If chars is given and not None, remove characters in chars instead.
Guido van Rossum20032041997-12-29 19:26:28 +000094
Barry Warsaw226ae6c1999-10-12 19:54:53 +000095 """
Neal Norwitzffe33b72003-04-10 22:35:32 +000096 return s.lstrip(chars)
Guido van Rossum306a8a61996-08-08 18:40:59 +000097
98# Strip trailing tabs and spaces
Neal Norwitzffe33b72003-04-10 22:35:32 +000099def rstrip(s, chars=None):
100 """rstrip(s [,chars]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000101
Neal Norwitzffe33b72003-04-10 22:35:32 +0000102 Return a copy of the string s with trailing whitespace removed.
103 If chars is given and not None, remove characters in chars instead.
Guido van Rossum20032041997-12-29 19:26:28 +0000104
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000105 """
Neal Norwitzffe33b72003-04-10 22:35:32 +0000106 return s.rstrip(chars)
Guido van Rossum306a8a61996-08-08 18:40:59 +0000107
108
Guido van Rossumc6360141990-10-13 19:23:40 +0000109# Split a string into a list of space/tab-separated words
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000110def split(s, sep=None, maxsplit=-1):
Fred Drakee4f13661999-11-04 19:19:48 +0000111 """split(s [,sep [,maxsplit]]) -> list of strings
Guido van Rossum20032041997-12-29 19:26:28 +0000112
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000113 Return a list of the words in the string s, using sep as the
Fred Drake14537542002-01-30 16:15:13 +0000114 delimiter string. If maxsplit is given, splits at no more than
115 maxsplit places (resulting in at most maxsplit+1 words). If sep
116 is not specified, any whitespace string is a separator.
Guido van Rossum20032041997-12-29 19:26:28 +0000117
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000118 (split and splitfields are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000119
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000120 """
121 return s.split(sep, maxsplit)
122splitfields = split
Guido van Rossumfac38b71991-04-07 13:42:19 +0000123
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000124# Split a string into a list of space/tab-separated words
125def rsplit(s, sep=None, maxsplit=-1):
126 """rsplit(s [,sep [,maxsplit]]) -> list of strings
127
128 Return a list of the words in the string s, using sep as the
129 delimiter string, starting at the end of the string and working
130 to the front. If maxsplit is given, at most maxsplit splits are
131 done. If sep is not specified or is None, any whitespace string
132 is a separator.
133 """
134 return s.rsplit(sep, maxsplit)
135
Guido van Rossum2ab19921995-06-22 18:58:00 +0000136# Join fields with optional separator
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000137def join(words, sep = ' '):
138 """join(list [,sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000139
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000140 Return a string composed of the words in list, with
Thomas Wouters7e474022000-07-16 12:04:32 +0000141 intervening occurrences of sep. The default separator is a
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000142 single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000143
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000144 (joinfields and join are synonymous)
Guido van Rossum20032041997-12-29 19:26:28 +0000145
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000146 """
147 return sep.join(words)
148joinfields = join
149
Guido van Rossumd3166071993-05-24 14:16:22 +0000150# Find substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000151def index(s, *args):
152 """index(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000153
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000154 Like find but raises ValueError when the substring is not found.
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.index(*args)
Guido van Rossumd3166071993-05-24 14:16:22 +0000158
Guido van Rossume65cce51993-11-08 15:05:21 +0000159# Find last substring, raise exception if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000160def rindex(s, *args):
161 """rindex(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000162
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000163 Like rfind but raises ValueError when the substring is not found.
Guido van Rossum20032041997-12-29 19:26:28 +0000164
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000165 """
Fred Drake046d2722000-07-03 07:23:13 +0000166 return s.rindex(*args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000167
168# Count non-overlapping occurrences of substring
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000169def count(s, *args):
170 """count(s, sub[, start[,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000171
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000172 Return the number of occurrences of substring sub in string
173 s[start:end]. Optional arguments start and end are
174 interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000175
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000176 """
Fred Drake046d2722000-07-03 07:23:13 +0000177 return s.count(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000178
Guido van Rossumd3166071993-05-24 14:16:22 +0000179# Find substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000180def find(s, *args):
181 """find(s, sub [,start [,end]]) -> in
Guido van Rossum20032041997-12-29 19:26:28 +0000182
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000183 Return the lowest index in s where substring sub is found,
184 such that sub is contained within s[start,end]. Optional
185 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000186
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000187 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000188
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000189 """
Fred Drake046d2722000-07-03 07:23:13 +0000190 return s.find(*args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000191
Guido van Rossume65cce51993-11-08 15:05:21 +0000192# Find last substring, return -1 if not found
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000193def rfind(s, *args):
194 """rfind(s, sub [,start [,end]]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000195
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000196 Return the highest index in s where substring sub is found,
197 such that sub is contained within s[start,end]. Optional
198 arguments start and end are interpreted as in slice notation.
Guido van Rossum20032041997-12-29 19:26:28 +0000199
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000200 Return -1 on failure.
Guido van Rossum20032041997-12-29 19:26:28 +0000201
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000202 """
Fred Drake046d2722000-07-03 07:23:13 +0000203 return s.rfind(*args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000204
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000205# for a bit of speed
206_float = float
207_int = int
208_long = long
Guido van Rossumd0753e21997-12-10 22:59:55 +0000209
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000210# Convert string to float
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000211def atof(s):
212 """atof(s) -> float
Guido van Rossum20032041997-12-29 19:26:28 +0000213
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000214 Return the floating point number represented by the string s.
Guido van Rossum20032041997-12-29 19:26:28 +0000215
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000216 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000217 return _float(s)
218
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000219
Guido van Rossumc6360141990-10-13 19:23:40 +0000220# Convert string to integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000221def atoi(s , base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000222 """atoi(s [,base]) -> int
Guido van Rossum20032041997-12-29 19:26:28 +0000223
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000224 Return the integer represented by the string s in the given
225 base, which defaults to 10. The string s must consist of one
226 or more digits, possibly preceded by a sign. If base is 0, it
227 is chosen from the leading characters of s, 0 for octal, 0x or
228 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
229 accepted.
Guido van Rossum20032041997-12-29 19:26:28 +0000230
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000231 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000232 return _int(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000233
Guido van Rossumc6360141990-10-13 19:23:40 +0000234
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000235# Convert string to long integer
Guido van Rossum9e896b32000-04-05 20:11:21 +0000236def atol(s, base=10):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000237 """atol(s [,base]) -> long
Guido van Rossum20032041997-12-29 19:26:28 +0000238
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000239 Return the long integer represented by the string s in the
240 given base, which defaults to 10. The string s must consist
241 of one or more digits, possibly preceded by a sign. If base
242 is 0, it is chosen from the leading characters of s, 0 for
243 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
244 0x or 0X is accepted. A trailing L or l is not accepted,
245 unless base is 0.
Guido van Rossum20032041997-12-29 19:26:28 +0000246
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000247 """
Guido van Rossum9e896b32000-04-05 20:11:21 +0000248 return _long(s, base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000249
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000250
Guido van Rossumc6360141990-10-13 19:23:40 +0000251# Left-justify a string
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000252def ljust(s, width, *args):
253 """ljust(s, width[, fillchar]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000254
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000255 Return a left-justified version of s, in a field of the
256 specified width, padded with spaces as needed. The string is
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000257 never truncated. If specified the fillchar is used instead of spaces.
Guido van Rossum20032041997-12-29 19:26:28 +0000258
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000259 """
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000260 return s.ljust(width, *args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000261
262# Right-justify a string
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000263def rjust(s, width, *args):
264 """rjust(s, width[, fillchar]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000265
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000266 Return a right-justified version of s, in a field of the
267 specified width, padded with spaces as needed. The string is
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000268 never truncated. If specified the fillchar is used instead of spaces.
Guido van Rossum20032041997-12-29 19:26:28 +0000269
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000270 """
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000271 return s.rjust(width, *args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000272
273# Center a string
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000274def center(s, width, *args):
275 """center(s, width[, fillchar]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000276
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000277 Return a center version of s, in a field of the specified
278 width. padded with spaces as needed. The string is never
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000279 truncated. If specified the fillchar is used instead of spaces.
Guido van Rossum20032041997-12-29 19:26:28 +0000280
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000281 """
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000282 return s.center(width, *args)
Guido van Rossumc6360141990-10-13 19:23:40 +0000283
284# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
285# Decadent feature: the argument may be a string or a number
286# (Use of this is deprecated; it should be a string as with ljust c.s.)
287def zfill(x, width):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000288 """zfill(x, width) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000289
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000290 Pad a numeric string x with zeros on the left, to fill a field
291 of the specified width. The string x is never truncated.
Guido van Rossum20032041997-12-29 19:26:28 +0000292
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000293 """
Walter Dörwald65230a22002-06-03 15:58:32 +0000294 if not isinstance(x, basestring):
Walter Dörwald068325e2002-04-15 13:36:47 +0000295 x = repr(x)
296 return x.zfill(width)
Guido van Rossum6ff2e901992-03-27 15:13:31 +0000297
298# Expand tabs in a string.
299# Doesn't take non-printing chars into account, but does understand \n.
Guido van Rossum894a7bb1995-08-10 19:42:05 +0000300def expandtabs(s, tabsize=8):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000301 """expandtabs(s [,tabsize]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000302
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000303 Return a copy of the string s with all tab characters replaced
304 by the appropriate number of spaces, depending on the current
305 column, and the tabsize (default 8).
Guido van Rossum20032041997-12-29 19:26:28 +0000306
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000307 """
Fred Drake046d2722000-07-03 07:23:13 +0000308 return s.expandtabs(tabsize)
Guido van Rossum2db91351992-10-18 17:09:59 +0000309
Guido van Rossum25395281996-05-28 23:08:45 +0000310# Character translation through look-up table.
Guido van Rossumed7253c1996-07-23 18:12:39 +0000311def translate(s, table, deletions=""):
Guido van Rossum5aff7752000-12-19 02:39:08 +0000312 """translate(s,table [,deletions]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000313
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000314 Return a copy of the string s, where all characters occurring
Guido van Rossum5aff7752000-12-19 02:39:08 +0000315 in the optional argument deletions are removed, and the
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000316 remaining characters have been mapped through the given
Guido van Rossum5aff7752000-12-19 02:39:08 +0000317 translation table, which must be a string of length 256. The
318 deletions argument is not allowed for Unicode strings.
Guido van Rossum20032041997-12-29 19:26:28 +0000319
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000320 """
Guido van Rossum5aff7752000-12-19 02:39:08 +0000321 if deletions:
322 return s.translate(table, deletions)
323 else:
324 # Add s[:0] so that if s is Unicode and table is an 8-bit string,
325 # table is converted to Unicode. This means that table *cannot*
326 # be a dictionary -- for that feature, use u.translate() directly.
327 return s.translate(table + s[:0])
Guido van Rossum2db91351992-10-18 17:09:59 +0000328
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000329# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
330def capitalize(s):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000331 """capitalize(s) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000332
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000333 Return a copy of the string s with only its first character
334 capitalized.
Guido van Rossum20032041997-12-29 19:26:28 +0000335
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000336 """
337 return s.capitalize()
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000338
339# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
340# See also regsub.capwords().
Guido van Rossum34f17311996-08-20 20:25:41 +0000341def capwords(s, sep=None):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000342 """capwords(s, [sep]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000343
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344 Split the argument into words using split, capitalize each
345 word using capitalize, and join the capitalized words using
346 join. Note that this replaces runs of whitespace characters by
347 a single space.
Guido van Rossum20032041997-12-29 19:26:28 +0000348
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000349 """
350 return join(map(capitalize, s.split(sep)), sep or ' ')
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000351
Guido van Rossumed7253c1996-07-23 18:12:39 +0000352# Construct a translation string
353_idmapL = None
354def maketrans(fromstr, tostr):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000355 """maketrans(frm, to) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000356
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000357 Return a translation table (a string of 256 bytes long)
358 suitable for use in string.translate. The strings frm and to
359 must be of the same length.
Guido van Rossum20032041997-12-29 19:26:28 +0000360
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000361 """
362 if len(fromstr) != len(tostr):
Fred Drake857c4c32000-02-10 16:21:11 +0000363 raise ValueError, "maketrans arguments must have same length"
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000364 global _idmapL
365 if not _idmapL:
Fred Drake857c4c32000-02-10 16:21:11 +0000366 _idmapL = map(None, _idmap)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000367 L = _idmapL[:]
368 fromstr = map(ord, fromstr)
369 for i in range(len(fromstr)):
Fred Drake857c4c32000-02-10 16:21:11 +0000370 L[fromstr[i]] = tostr[i]
Eric S. Raymonde37340e2001-02-09 16:56:44 +0000371 return join(L, "")
Guido van Rossum8775d8b1996-06-11 18:43:00 +0000372
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000373# Substring replacement (global)
Guido van Rossum8f0c5a72000-03-10 23:22:10 +0000374def replace(s, old, new, maxsplit=-1):
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000375 """replace (str, old, new[, maxsplit]) -> string
Guido van Rossum20032041997-12-29 19:26:28 +0000376
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000377 Return a copy of string str with all occurrences of substring
378 old replaced by new. If the optional argument maxsplit is
379 given, only the first maxsplit occurrences are replaced.
Guido van Rossum20032041997-12-29 19:26:28 +0000380
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000381 """
382 return s.replace(old, new, maxsplit)
Guido van Rossum1eb9a811997-03-25 16:50:31 +0000383
384
Guido van Rossum2db91351992-10-18 17:09:59 +0000385# Try importing optional built-in module "strop" -- if it exists,
386# it redefines some string operations that are 100-1000 times faster.
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000387# It also defines values for whitespace, lowercase and uppercase
388# that match <ctype.h>'s definitions.
Guido van Rossum2db91351992-10-18 17:09:59 +0000389
390try:
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000391 from strop import maketrans, lowercase, uppercase, whitespace
392 letters = lowercase + uppercase
Guido van Rossumb6775db1994-08-01 11:34:53 +0000393except ImportError:
Fred Drake857c4c32000-02-10 16:21:11 +0000394 pass # Use the original versions