Two simple-minded capitalization functions for string.py

Skip Montanaro (skip@automatrix.com)
Mon, 13 Feb 1995 21:11:40 -0500

Here are two rather simple-minded functions for string.py to capitalize a
string or a string consisting of several whitespace-separated words. My
definition of a capitalized word is one in which the first letter is
uppercase and the rest are lower. Some folks may not agree with lowercase
shifting the rest of the characters. I suppose I could have gotten more
elaborate, such as capitalizing initials (r.e.m. -> R.E.M.), but it's not
necessary for my use, so I let it slide. It also destroys embedded capitals
(VanRossum -> Vanrossum). Maybe after a little roundabout in the group one
or more functions will be converged on that can be added to string.py.

# capitalize a string consisting of one or more whitespace-separated words
def capitalize_words(s):
s = split(s)
for i in range(len(s)):
s[i] = capitalize(s[i])
return join(s)

# capitalize a string (first char upper, rest lower)
def capitalize(s):
if s == '': return s
return upper(s[0]) + lower(s[1:])

--
Skip Montanaro		skip@automatrix.com			  (518)372-5583
Musi-Cal: http://www.calendar.com/concerts/ -or- concerts@calendar.com
Internet Conference Calendar: http://www.calendar.com/conferences/