Count Word Occurrences
Beginner · Write the code · from Every Python String Method Explained with Examples
From the lesson: Every Python String Method Explained with Examples
Strings in Python come with a toolbox of built-in methods. These methods let you change case, remove extra spaces, search for words, split text apart, and much more.
Read the lessonCount how many times the word "the" appears in the sentence below. Use the count() method.
Print the result in this exact format:
the appears 4 timesHint: Convert to lowercase first so you catch both "the" and "The".
sentence = "The cat sat on the mat by the door and The dog watched"
# Count how many times "the" appears and print it
More string methods exercises
- Clean Up User InputBeginner
- Replace All OccurrencesBeginner
- Split a CSV LineBeginner
- Fix the Bug -- find() vs index()Beginner
- Validate a UsernameBeginner
All Python exercises · 50 practice problems · Python Fundamentals quiz