Skip to main content
 首页 » 编程设计

Python 的 String 类的 count() 方法

2025年05月04日9dudu

我正在阅读 Mark Pilgrim 的 Dive into 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,但没有解释原因。

有人能帮帮我吗?谢谢!

请您参考如下方法:

该语句中有 6 个 f。您可能不小心忽略了“of”。

(实际上,这是 a widely circulated brainteaser)。