Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

'capwords' is not a string method

1,408 views
Skip to first unread message

the...@binary.net

unread,
Jul 17, 2002, 5:23:51 PM7/17/02
to

Here is something I very recently discovered . . . "capwords" are not
included as a method of the string type, but "upcase" is:

>>> str = "python rocks"
>>> str.capwords()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'capwords'
>>> str.upper()
'PYTHON ROCKS'


But "capwords" *is* included in the string module:

>>> string.capwords(str)
'Python Rocks'

So I can still do it, but this seems a little inconsistant to me. Is
there a reason, or is it just an oversight??

-- mikeh

--
Mike Hostetler
the...@binary.net
http://www.binary.net/thehaas
GnuPG key: http://www.binary.net/thehaas/mikeh.gpg

Alex Martelli

unread,
Jul 17, 2002, 5:42:15 PM7/17/02
to
the...@binary.net wrote:
...

> But "capwords" *is* included in the string module:
>
>>>> string.capwords(str)
> 'Python Rocks'
>
> So I can still do it, but this seems a little inconsistant to me. Is
> there a reason, or is it just an oversight??
>
> -- mikeh

>>> import string
>>> string.capwords('python/rocks')
'Python/rocks'
>>> 'python/rocks'.title()
'Python/Rocks'
>>>

Apparently, they took occasion of the introduction of string
methods to replace the semi-crippled capwords (which only
capitalizes words _preceded by whitespace_) with the better
method title, which capitalizes words even when they are
preceded by punctuation, as well as whitespace. It seems
sensible to me -- the need met by .title() is more frequent,
and if the issue is backwards compatibility, capwords is
still around (in the semi-obsolescent string module -- only
semi, because it supplies crucial constants such as digits,
letters, whitespace, etc).


Alex

Sean 'Shaleh' Perry

unread,
Jul 17, 2002, 5:34:04 PM7/17/02
to

On 17-Jul-2002 the...@binary.net wrote:
>
> Here is something I very recently discovered . . . "capwords" are not
> included as a method of the string type, but "upcase" is:
>

my guess is the implementation for Unicode was not straight forward. Or
perhaps they felt that not every method of the string library needed to be
implemented on the new objects.


the...@binary.net

unread,
Jul 17, 2002, 6:38:19 PM7/17/02
to
Alex Martelli <al...@aleax.it> wrote:
> Apparently, they took occasion of the introduction of string
> methods to replace the semi-crippled capwords (which only
> capitalizes words _preceded by whitespace_) with the better
> method title, which capitalizes words even when they are
> preceded by punctuation, as well as whitespace. It seems
> sensible to me -- the need met by .title() is more frequent,
> and if the issue is backwards compatibility, capwords is
> still around (in the semi-obsolescent string module -- only
> semi, because it supplies crucial constants such as digits,
> letters, whitespace, etc).

I didn't know about the 'title' method . . .I'll try that. I'd rather
use string's built-in methods than (how you put it) the semi-obsolescent
string module.

0 new messages