| 1 | # module 'string' -- A collection of string operations
|
|---|
| 2 |
|
|---|
| 3 | # 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.
|
|---|
| 7 |
|
|---|
| 8 | """Common string manipulations.
|
|---|
| 9 |
|
|---|
| 10 | Public module variables:
|
|---|
| 11 |
|
|---|
| 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
|
|---|
| 19 |
|
|---|
| 20 | """
|
|---|
| 21 |
|
|---|
| 22 | # Some strings for ctype-style character classification
|
|---|
| 23 | whitespace = ' \t\n\r\v\f'
|
|---|
| 24 | lowercase = 'abcdefghijklmnopqrstuvwxyz'
|
|---|
| 25 | uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|---|
| 26 | letters = lowercase + uppercase
|
|---|
| 27 | digits = '0123456789'
|
|---|
| 28 | hexdigits = digits + 'abcdef' + 'ABCDEF'
|
|---|
| 29 | octdigits = '01234567'
|
|---|
| 30 |
|
|---|
| 31 | # Case conversion helpers
|
|---|
| 32 | _idmap = ''
|
|---|
| 33 | for i in range(256): _idmap = _idmap + chr(i)
|
|---|
| 34 | del i
|
|---|
| 35 |
|
|---|
| 36 | # Backward compatible names for exceptions
|
|---|
| 37 | index_error = ValueError
|
|---|
| 38 | atoi_error = ValueError
|
|---|
| 39 | atof_error = ValueError
|
|---|
| 40 | atol_error = ValueError
|
|---|
| 41 |
|
|---|
| 42 | # convert UPPER CASE letters to lower case
|
|---|
| 43 | def lower(s):
|
|---|
| 44 | """lower(s) -> string
|
|---|
| 45 |
|
|---|
| 46 | Return a copy of the string s converted to lowercase.
|
|---|
| 47 |
|
|---|
| 48 | """
|
|---|
| 49 | return s.lower()
|
|---|
| 50 |
|
|---|
| 51 | # Convert lower case letters to UPPER CASE
|
|---|
| 52 | def upper(s):
|
|---|
| 53 | """upper(s) -> string
|
|---|
| 54 |
|
|---|
| 55 | Return a copy of the string s converted to uppercase.
|
|---|
| 56 |
|
|---|
| 57 | """
|
|---|
| 58 | return s.upper()
|
|---|
| 59 |
|
|---|
| 60 | # Swap lower case letters and UPPER CASE
|
|---|
| 61 | def swapcase(s):
|
|---|
| 62 | """swapcase(s) -> string
|
|---|
| 63 |
|
|---|
| 64 | Return a copy of the string s with upper case characters
|
|---|
| 65 | converted to lowercase and vice versa.
|
|---|
| 66 |
|
|---|
| 67 | """
|
|---|
| 68 | return s.swapcase()
|
|---|
| 69 |
|
|---|
| 70 | # Strip leading and trailing tabs and spaces
|
|---|
| 71 | def strip(s):
|
|---|
| 72 | """strip(s) -> string
|
|---|
| 73 |
|
|---|
| 74 | Return a copy of the string s with leading and trailing
|
|---|
| 75 | whitespace removed.
|
|---|
| 76 |
|
|---|
| 77 | """
|
|---|
| 78 | return s.strip()
|
|---|
| 79 |
|
|---|
| 80 | # Strip leading tabs and spaces
|
|---|
| 81 | def lstrip(s):
|
|---|
| 82 | """lstrip(s) -> string
|
|---|
| 83 |
|
|---|
| 84 | Return a copy of the string s with leading whitespace removed.
|
|---|
| 85 |
|
|---|
| 86 | """
|
|---|
| 87 | return s.lstrip()
|
|---|
| 88 |
|
|---|
| 89 | # Strip trailing tabs and spaces
|
|---|
| 90 | def rstrip(s):
|
|---|
| 91 | """rstrip(s) -> string
|
|---|
| 92 |
|
|---|
| 93 | Return a copy of the string s with trailing whitespace
|
|---|
| 94 | removed.
|
|---|
| 95 |
|
|---|
| 96 | """
|
|---|
| 97 | return s.rstrip()
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 | # Split a string into a list of space/tab-separated words
|
|---|
| 101 | def split(s, sep=None, maxsplit=0):
|
|---|
| 102 | """split(str [,sep [,maxsplit]]) -> list of strings
|
|---|
| 103 |
|
|---|
| 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.
|
|---|
| 108 |
|
|---|
| 109 | (split and splitfields are synonymous)
|
|---|
| 110 |
|
|---|
| 111 | """
|
|---|
| 112 | return s.split(sep, maxsplit)
|
|---|
| 113 | splitfields = split
|
|---|
| 114 |
|
|---|
| 115 | # Join fields with optional separator
|
|---|
| 116 | def join(words, sep = ' '):
|
|---|
| 117 | """join(list [,sep]) -> string
|
|---|
| 118 |
|
|---|
| 119 | Return a string composed of the words in list, with
|
|---|
| 120 | intervening occurrences of sep. The default separator is a
|
|---|
| 121 | single space.
|
|---|
| 122 |
|
|---|
| 123 | (joinfields and join are synonymous)
|
|---|
| 124 |
|
|---|
| 125 | """
|
|---|
| 126 | return sep.join(words)
|
|---|
| 127 | joinfields = join
|
|---|
| 128 |
|
|---|
| 129 | # for a little bit of speed
|
|---|
| 130 | _apply = apply
|
|---|
| 131 |
|
|---|
| 132 | # Find substring, raise exception if not found
|
|---|
| 133 | def index(s, *args):
|
|---|
| 134 | """index(s, sub [,start [,end]]) -> int
|
|---|
| 135 |
|
|---|
| 136 | Like find but raises ValueError when the substring is not found.
|
|---|
| 137 |
|
|---|
| 138 | """
|
|---|
| 139 | return _apply(s.index, args)
|
|---|
| 140 |
|
|---|
| 141 | # Find last substring, raise exception if not found
|
|---|
| 142 | def rindex(s, *args):
|
|---|
| 143 | """rindex(s, sub [,start [,end]]) -> int
|
|---|
| 144 |
|
|---|
| 145 | Like rfind but raises ValueError when the substring is not found.
|
|---|
| 146 |
|
|---|
|
|---|