.title() - annoying mistake

Cameron Simpson cs at cskk.id.au
Sat Mar 20 01:52:39 EDT 2021


On 19Mar2021 23:47, Abdur-Rahmaan Janhangeer <arj.python at gmail.com> wrote:
>At least i'd expect what it pretends to do
>even if not following English.
>
>Missing ' is a weird behaviour, i get it
>they skipped every non lettet

I think the lesson here is that .title() doesn't even do English 
language title capitalisation, and you shouldn't expect it to. That's a 
context dependent vocabulary dependent thing and there's plenty of 
variations on how you might like to do it. So expecting a naive context 
free method to do what you want is unwise.

The .title method does something which resembles English capitalisation 
in simple cases. Rather than naming it .simplistic_if_you_are_lucky it 
was named .title because in some simple cases it does do what you might 
want: _read_ its specification and recognise that that is _not_ a 
natural language capable function, but s simple lexical function which 
toggles the leading character of a simple approximation of "words".

_If_ you're going to use it with natural language, its simplicity 
implies that the user needs to do some preparsing of the text to decide 
where this function can be used to get correct results.

Don't get hung up that it didn't do what you want, recognise that it 
does something simple and work with that limitation. Or make your own, 
likely as part of a more complex library with deeper understanding of 
language.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list