.title() - annoying mistake

Chris Angelico rosuav at gmail.com
Mon Mar 22 10:22:23 EDT 2021


On Tue, Mar 23, 2021 at 1:18 AM Grant Edwards <grant.b.edwards at gmail.com> wrote:
>
> On 2021-03-21, MRAB <python at mrabarnett.plus.com> wrote:
>
> IMO, the doc is wrong.
>
> >> 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 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], 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.

ChrisA


More information about the Python-list mailing list