capwords (WAS: [Newby] question about modules)

Steven Bethard steven.bethard at gmail.com
Fri Dec 10 21:22:07 CET 2004


Jon wrote:
> As far as I can tell from the online docs, "capwords" should be defined in
> the built-in "regex" module.  Why is it telling me that capwords is not
> defined?

Hmm... are you looking instead for "capwords" from the string module?

 >>> s = """\
... Well, he's...
... he's, ah...
... probably pining for the fjords."""
 >>> import string
 >>> print string.capwords(s)
Well, He's... He's, Ah... Probably Pining For The Fjords.
 >>> print s.title()
Well, He'S...
He'S, Ah...
Probably Pining For The Fjords.

Note that there are a few subtle differences between string.capwords and 
str.title -- string.capwords capitalizes only at whitespace boundaries 
(and replaces runs of whitespace with spaces), while str.title 
capitalizes at alphanumeric boundaries.

Steve



More information about the Python-list mailing list