python – 如果字符串显然是title-case,为什么istitle()字符串方法返回false?

在istitle()字符串方法中,Python 2.6.5手册中包含:

Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise.

但在这种情况下,它返回false:

>>> book = 'what every programmer must know'
>>> book.title()
'What Every Programmer Must Know'
>>> book.istitle()
False

我错过了什么?

最佳答案
方法title()不会改变字符串(字符串在Python中是不可变的).它会创建一个必须分配给变量的新字符串:

>>> book = 'what every programmer must know'
>>> book = book.title()
>>> book.istitle()
True

转载注明原文:python – 如果字符串显然是title-case,为什么istitle()字符串方法返回false? - 代码日志