.title() - annoying mistake

Richard Damon Richard at Damon-Family.org
Sun Mar 21 22:59:24 EDT 2021


On 3/21/21 10:28 PM, Chris Angelico wrote:
> On Mon, Mar 22, 2021 at 12:26 PM Richard Damon <Richard at damon-family.org> wrote:
>> On 3/21/21 7:31 PM, MRAB wrote:
>>> On 2021-03-21 22:30, Chris Angelico wrote:
>>>> On Mon, Mar 22, 2021 at 9:04 AM Grant Edwards
>>>> <grant.b.edwards at gmail.com> wrote:
>>>>> On 2021-03-21, Chris Angelico <rosuav at gmail.com> wrote:
>>>>>> On Mon, Mar 22, 2021 at 2:16 AM Robert Latest via Python-list
>>>>> <python-list at python.org> wrote:
>>>>>>> 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.
>>>>> Not according to the docs. The doc states that .title() converts the
>>>>> first character characger in each "word" to _upper_ case. Is the doc
>>>>> wrong?
>>>>>
>>>>> If you want titlecase, then you should call str.capitalize() which
>>>>> (again according to the doc) converts the first character to _title_
>>>>> case (starting in v3.8).
>>>>>
>>>> Hmm, maybe it's different in 3.10, but the docs I'm seeing look fine.
>>>> But maybe there's a better way to word it for both of them.
>>>>
>>> Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928
>>> 64 bit (AMD64)] on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> help(str.title)
>>> Help on method_descriptor:
>>>
>>> title(self, /)
>>>     Return a version of the string where each word is titlecased.
>>>
>>>     More specifically, words start with uppercased characters and all
>>> remaining
>>>     cased characters have lower case.
>>>
>>> '\N{LATIN CAPITAL LETTER DZ}', '\N{LATIN SMALL LETTER DZ}' and
>>> '\N{LATIN CAPITAL LETTER D WITH SMALL LETTER Z}' are all digraphs, so
>>> is it correct to say that .title() uppercases the first character?
>>> Kind of.
>> I think the clarification calling them upper cased characters is close
>> enough considering that there are only 31 title cased characters, all
>> digraphs.
>>
> But it's wrong, and it would lead people to the exact error of
> thinking that it's the same as upper() on str[0] and lower() on the
> rest.
>
> ChrisA

If it didn't mention that it was generating a 'titlecase' that could be
an argument, but since for 99.99% of characters Title Casing is
identical to upper case (and that character IS called the upper case),
but for the 31 listed digraphs, it means the titlecase version of that
digraph where the first 'letter' in the digraph is like the upper case
of its equivalent, and the second 'letter' in the digraph is like the
lower case of its equivalent.

Basically, titlecasing a word IS making the first letter upper case and
the rest lower case UNLESS the first letter is on of the 31 digraphs
which have a special titlecase version, then that is used for the first
letter. That gets pretty wordy for an explanation string.

-- 
Richard Damon



More information about the Python-list mailing list