Editor

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 lesson

Count 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 times

Hint: 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

All Python exercises · 50 practice problems · Python Fundamentals quiz