[Newby] question about modules

Steven Bethard steven.bethard at gmail.com
Fri Dec 10 15:12:38 EST 2004


Jon wrote:
> The following four lines of code:
> 
> import sys, os, re
> sentence = raw_input("Enter a sentence:  ")
> capwords (sentence)
> print sentence
> 
> gives me the following error:  NameError: name 'capwords' is not defined
> 
> 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?

Which docs are you looking at?  I couldn't find documentation for a 
capwords method or class...  Checking at the interactive prompt:

 >>> import regex
__main__:1: DeprecationWarning: the regex module is deprecated; please 
use the re module
 >>> dir(regex)
['__doc__', '__name__', 'casefold', 'compile', 'error', 'get_syntax', 
'match', 'search', 'set_syntax', 'symcomp']
 >>> import re
 >>> dir(re)
['DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'U', 
'UNICODE', 'VERBOSE', 'X', '__all__', '__builtins__', '__doc__', 
'__file__', '__name__', 'compile', 'engine', 'error', 'escape', 
'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sub', 
'subn', 'template']

I don't see an object named capwords in either module.

What are you trying to do?  If you just want to capitalize words, 
str.title() is probably the easier way to do this:

 >>> s = "i wanted to be a lumberjack!"
 >>> s.title()
'I Wanted To Be A Lumberjack!'

Steve



More information about the Python-list mailing list