Python的String类count()方法

时间:2012-05-04 18:20:42

标签: python

我正在阅读Mark Pilgrim的潜水到Python 3并且现在已经盯着它看了大约一个小时:http://getpython3.com/diveintopython3/strings.html#common-string-methods

>>> s = '''Finished files are the re-
... sult of years of scientif-
... ic study combined with the
... experience of years.'''
>>> s.lower().count('f')
6

在给出的多行字符串示例中,我不明白为什么s.lower().count('f')返回6而不是3.我已经确认它确实返回6.当然,Pilgrim甚至在他的笔记中指出它实际上是6,但不解释原因。

有人可以帮帮我吗?谢谢!

4 个答案:

答案 0 :(得分:5)

该声明中有6个。你可能会意外地忽略了“of”。

(的确,这是a widely circulated brainteaser)。

答案 1 :(得分:5)

...... ......的科学......的文件......

答案 2 :(得分:4)

这可能是一个有用的视觉效果。

Six f's

答案 3 :(得分:2)

F inished f iles是 f f scienti f ic研究结合经验o f 年。

'lower'使得整个字符串都是小写的,所以一定要包含“Finished”之类的内容。