Jeff Erickson
Jeff Erickson

Reputation: 3893

Python's String class count() method

I'm reading Mark Pilgrim's Dive into Python 3 and have been staring at this for about an hour now: 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

In the multi-line string example given, I don't understand why s.lower().count('f') returns 6 instead of 3. I have confirmed that it does return 6. Of course, Pilgrim even points out in his notes that it is in fact 6, but doesn't explain why.

Can someone help me out? Thanks!

Upvotes: 1

Views: 676

Answers (4)

Andrew Clark
Andrew Clark

Reputation: 208465

This might be a helpful visual.

Six f's

Upvotes: 4

David Robinson
David Robinson

Reputation: 78590

There are 6 f's in that statement. You might be accidentally ignoring the "of"s.

(Indeed, this is a widely circulated brainteaser).

Upvotes: 5

Jonathan Sternberg
Jonathan Sternberg

Reputation: 6647

Finished files are the result of years of scientific study combined with the experience of years.

The 'lower' makes it so the entire string is lowercase, so make sure to include things like "Finished".

Upvotes: 2

krousey
krousey

Reputation: 1798

Finished... files... of... of... scientific... of...

Upvotes: 5

Related Questions