.title() - annoying mistake

Grant Edwards grant.b.edwards at gmail.com
Mon Mar 22 10:38:30 EDT 2021


On 2021-03-22, Chris Angelico <rosuav at gmail.com> wrote:
> On Tue, Mar 23, 2021 at 1:18 AM Grant Edwards <grant.b.edwards at gmail.com> wrote:
>
>> I guess it depends on what you mean by "character". In my mind, the
>> first character of string s is s[1], and I would then expect that
>>
>> s.title()[1] == s[1].upper()
>>
> I presume you mean [0],

Yes.

> but no, that's not the case. A single character can titlecase to two
> characters, or to a single character that isn't the same as if you
> uppercase or lowercase it. See examples in previous post.

The document for str.title() states that the initial character of each
word is converted to uppercase.  My point is that for characters that
remain single characters regardless of case that means (to me) that

  s.title()[0] == s[0].upper()

or for a single character string

  s.title() == s.upper()

That's not true for digraphs where there is a third, distinct and
different "title" case. I think the doc should state that the initial
character is converted to titlecase. A parenthentical statement that
titlecase is usually but not always equal to uppercase would be nice,
but the current statement is obsolete and not correct in all, um...
cases.

--
Grant




More information about the Python-list mailing list