在istitle()字符串方法中,Python2.6.5手册读取:
如果字符串是以标题为标题的字符串,并且至少有一个字符,则
返回true,例如,大写字符只能跟随未大写字符,小写字符只能大写字符。否则返回错误。
但在这种情况下,它返回false:
>>> book = 'what every programmer must know'
>>> book.title()
'What Every Programmer Must Know'
>>> book.istitle()
False我遗漏了什么?
发布于 2010-07-27 13:42:29
book.title()不更改变量book。它只返回标题大小写中的字符串。
>>> book.title()
'What Every Programmer Must Know'
>>> book # still not in title case
'what every programmer must know'
>>> book.istitle() # hence it returns False.
False
>>> book.title().istitle() # returns True as expected
True发布于 2010-07-27 13:42:07
可能是因为您仍在调用原始书上的istitle()。
尝试book.title().istitle()代替..。
发布于 2010-07-27 13:43:13
做以下工作:
print book在你做book.title()之后。您将看到book并没有改变。
原因是book.title()创建了一个新字符串。名称book仍然引用原始字符串。
https://stackoverflow.com/questions/3344218
复制相似问题