Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Basic utility functions
4. The count() function
Basic manipulation functions
Replacing and splitting
Summary

Instruction

Well done. The next function is count(). It returns the number of occurrences of a given substring in a given string. Take a look:

paragraph = input('Please provide a paragraph of text: ')
full_stop_counter = paragraph.count('.')
print('It seems your paragraph contains', str(full_stop_counter), 'sentences!')

The code above counts the number of full stops (periods) in the paragraph variable and uses this figure to find the number of sentences in the paragraph. It's far from being perfect, but it illustrates how you can use count() in your code.

Exercise

You are given a snippet taken from a server log file. It contains three levels of messages: INFO, WARNING, and ERROR. Analyze the logs using count(), and print the following two lines:

Number of errors: {x}
Number of warnings: {y}

Stuck? Here's a hint!

To find the number of errors, use

logs.count('ERROR')