The count() method returns the number of occurrences of a substring in the given string.
Example
Syntax of String count
The syntax of count() method is:
string.count(substring, start=..., end=...)
count() Parameters
count() method only requires a single parameter for execution. However, it also has two optional parameters:
- substring - string whose count is to be found.
- start (Optional) - starting index within the string where search starts.
- end (Optional) - ending index within the string where search ends.
Note: Index in Python starts from 0, not 1.
count() Return Value
count() method returns the number of occurrences of the substring in the given string.
Example 1: Count number of occurrences of a given substring
Output
The count is: 2
Example 2: Count number of occurrences of a given substring using start and end
Output
The count is: 1
Here, the counting starts after the first i has been encountered, i.e. 7th index position.
And, it ends before the last i, i.e. 25th index position.