source: trunk/essentials/dev-lang/python/Lib/stringold.py@ 3226

Last change on this file since 3226 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 12.0 KB
Line 
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
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
22# Some strings for ctype-style character classification
23whitespace = ' \t\n\r\v\f'
24lowercase = 'abcdefghijklmnopqrstuvwxyz'
25uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
26letters = lowercase + uppercase
27digits = '0123456789'
28hexdigits = digits + 'abcdef' + 'ABCDEF'
29octdigits = '01234567'
30
31# Case conversion helpers
32_idmap = ''
33for i in range(256): _idmap = _idmap + chr(i)
34del i
35
36# Backward compatible names for exceptions
37index_error = ValueError
38atoi_error = ValueError
39atof_error = ValueError
40atol_error = ValueError
41
42# convert UPPER CASE letters to lower case
43def 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
52def 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
61def 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
71def 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
81def 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
90def 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
101def 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)
113splitfields = split
114
115# Join fields with optional separator
116def 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)
127joinfields = join
128
129# for a little bit of speed
130_apply = apply
131
132# Find substring, raise exception if not found
133def 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
142def rindex(s, *args):
143 """rindex(s, sub [,start [,end]]) -> int
144
145 Like rfind but raises ValueError when the substring is not found.
146