8 """Common string manipulations.
10 Public module variables:
12 whitespace -- a string containing all characters considered whitespace
13 lowercase -- a string containing all characters considered lowercase letters
14 uppercase -- a string containing all characters considered uppercase letters
15 letters -- a string containing all characters considered letters
16 digits -- a string containing all characters considered decimal digits
17 hexdigits -- a string containing all characters considered hexadecimal digits
18 octdigits -- a string containing all characters considered octal digits
23 whitespace =
' \t\n\r\v\f'
24 lowercase =
'abcdefghijklmnopqrstuvwxyz'
25 uppercase =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
26 letters = lowercase + uppercase
28 hexdigits = digits +
'abcdef' +
'ABCDEF'
29 octdigits =
'01234567'
33 for i
in range(256): _idmap = _idmap + chr(i)
37 index_error = ValueError
38 atoi_error = ValueError
39 atof_error = ValueError
40 atol_error = ValueError
46 Return a copy of the string s converted to lowercase.
55 Return a copy of the string s converted to uppercase.
62 """swapcase(s) -> string
64 Return a copy of the string s with upper case characters
65 converted to lowercase and vice versa.
74 Return a copy of the string s with leading and trailing
82 """lstrip(s) -> string
84 Return a copy of the string s with leading whitespace removed.
91 """rstrip(s) -> string
93 Return a copy of the string s with trailing whitespace
102 """split(str [,sep [,maxsplit]]) -> list of strings
104 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.
109 (split and splitfields are synonymous)
112 return s.split(sep, maxsplit)
117 """join(list [,sep]) -> string
119 Return a string composed of the words in list, with
120 intervening occurrences of sep. The default separator is a
123 (joinfields and join are synonymous)
126 return sep.join(words)
134 """index(s, sub [,start [,end]]) -> int
136 Like find but raises ValueError when the substring is not found.
139 return _apply(s.index, args)
143 """rindex(s, sub [,start [,end]]) -> int
145 Like rfind but raises ValueError when the substring is not found.
148 return _apply(s.rindex, args)
152 """count(s, sub[, start[,end]]) -> int
154 Return the number of occurrences of substring sub in string
155 s[start:end]. Optional arguments start and end are
156 interpreted as in slice notation.
159 return _apply(s.count, args)
163 """find(s, sub [,start [,end]]) -> in
165 Return the lowest index in s where substring sub is found,
166 such that sub is contained within s[start,end]. Optional
167 arguments start and end are interpreted as in slice notation.
169 Return -1 on failure.
172 return _apply(s.find, args)
176 """rfind(s, sub [,start [,end]]) -> int
178 Return the highest index in s where substring sub is found,
179 such that sub is contained within s[start,end]. Optional
180 arguments start and end are interpreted as in slice notation.
182 Return -1 on failure.
185 return _apply(s.rfind, args)
191 _StringType = type(
'')
197 Return the floating point number represented by the string s.
200 if type(s) == _StringType:
203 raise TypeError(
'argument 1: expected string, %s found' %
208 """atoi(s [,base]) -> int
210 Return the integer represented by the string s in the given
211 base, which defaults to 10. The string s must consist of one
212 or more digits, possibly preceded by a sign. If base is 0, it
213 is chosen from the leading characters of s, 0 for octal, 0x or
214 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
221 raise TypeError(
'function requires at least 1 argument: %d given' %
226 if type(s) == _StringType:
229 raise TypeError(
'argument 1: expected string, %s found' %
235 """atol(s [,base]) -> long
237 Return the long integer represented by the string s in the
238 given base, which defaults to 10. The string s must consist
239 of one or more digits, possibly preceded by a sign. If base
240 is 0, it is chosen from the leading characters of s, 0 for
241 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
242 0x or 0X is accepted. A trailing L or l is not accepted,
249 raise TypeError(
'function requires at least 1 argument: %d given' %
254 if type(s) == _StringType:
255 return _apply(_long, args)
257 raise TypeError(
'argument 1: expected string, %s found' %
263 """ljust(s, width) -> string
265 Return a left-justified version of s, in a field of the
266 specified width, padded with spaces as needed. The string is
276 """rjust(s, width) -> string
278 Return a right-justified version of s, in a field of the
279 specified width, padded with spaces as needed. The string is
289 """center(s, width) -> string
291 Return a center version of s, in a field of the specified
292 width. padded with spaces as needed. The string is never
302 return ' '*half + s +
' '*(n-half)
308 """zfill(x, width) -> string
310 Pad a numeric string x with zeros on the left, to fill a field
311 of the specified width. The string x is never truncated.
314 if type(x) == type(
''): s = x
317 if n >= width:
return s
319 if s[0]
in (
'-',
'+'):
320 sign, s = s[0], s[1:]
321 return sign +
'0'*(width-n) + s
326 """expandtabs(s [,tabsize]) -> string
328 Return a copy of the string s with all tab characters replaced
329 by the appropriate number of spaces, depending on the current
330 column, and the tabsize (default 8).
336 c =
' '*(tabsize - len(line) % tabsize)
345 """translate(s,table [,deletechars]) -> string
347 Return a copy of the string s, where all characters occurring
348 in the optional argument deletechars are removed, and the
349 remaining characters have been mapped through the given
350 translation table, which must be a string of length 256.
353 return s.translate(table, deletions)
357 """capitalize(s) -> string
359 Return a copy of the string s with only its first character
363 return s.capitalize()
368 """capwords(s, [sep]) -> string
370 Split the argument into words using split, capitalize each
371 word using capitalize, and join the capitalized words using
372 join. Note that this replaces runs of whitespace characters by
376 return join(map(capitalize, s.split(sep)), sep
or ' ')
381 """maketrans(frm, to) -> string
383 Return a translation table (a string of 256 bytes long)
384 suitable for use in string.translate. The strings frm and to
385 must be of the same length.
388 if len(fromstr) != len(tostr):
389 raise ValueError,
"maketrans arguments must have same length"
392 _idmapL = map(
None, _idmap)
394 fromstr = map(ord, fromstr)
395 for i
in range(len(fromstr)):
396 L[fromstr[i]] = tostr[i]
401 """replace (str, old, new[, maxsplit]) -> string
403 Return a copy of string str with all occurrences of substring
404 old replaced by new. If the optional argument maxsplit is
405 given, only the first maxsplit occurrences are replaced.
408 return s.replace(old, new, maxsplit)
418 except AttributeError:
419 from stringold
import *
427 from strop
import maketrans, lowercase, uppercase, whitespace
428 letters = lowercase + uppercase