Given a string shakespeare containing
The quick brown fox jumps over the lazy dog thE.
THE other dog ate tHe thing .the but not there
# Use Python's built-in string containing all punctuation
from string import punctuation
count = 0
# .lower() makes the whole string lowercase, so you don't have to worry about
# case anymore
# .split() separates the string into a list of things in between whitespace
for word in <censored>
# .strip() removes all the characters in the argument (punctuation) from
# the front and end of the string
if <censored>:
count += 1
print(count)
Will print 6
Edit: I took out the complete solution
If you want to use functional programming, that ^ can be simplified to
from string import punctuation
print(sum([word.strip(punctuation) == 'the' \
for word in shakespeare.lower().split()]))
If you're allowed to use regexes it's a lot easier:
import re
print(len(re.findall(r'(?i)\bthe\b',shakespeare)))