.title() - annoying mistake

Chris Angelico rosuav at gmail.com
Sun Mar 21 11:26:30 EDT 2021


On Mon, Mar 22, 2021 at 2:16 AM Robert Latest via Python-list
<python-list at python.org> wrote:
>
> Chris Angelico wrote:
> > On Sun, Mar 21, 2021 at 10:31 PM Robert Latest via Python-list
> ><python-list at python.org> wrote:
> >> Yes, I get that. But the purpose it (improperly) serves only makes sense in
> >> the English language.
> >
> > Why? Do titles not exist in other languages? Does no other language
> > capitalize words in book or other titles?
>
> I wonder if .title() properly capitalizes titles in any language. It doesn't in
> English (nor does it purport to), so it begs the question why it is there in
> the first place. German and Spanish don't have any special capitalization rules
> for titles; I don't know about any other languages.
>

It correctly title-cases a single character, as has been pointed out
already. Attempting to do this with upper() or lower() will give
incorrect results. So if you want to define language-specific rules
(maybe with a regex) for splitting into words and subwords, you can
then use title() and lower() to perform the actual changes.

Treat it as a building-block rather than as a magical "do what I want"
function, and it is incredibly useful, and in fact, is the only way to
be correct.

ChrisA


More information about the Python-list mailing list