The string count() method returns the number of occurrences of a substring in the given string.
Example
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
str = "Happy Birthday, My Dear Friend!"
print('The number of character a: ', str.count('a'))
print('The number of character i: ', str.count('i'))
print('The number of character y: ', str.count('y'))
print('The number of character k: ', str.count('k'))
# count is case sensitive
print('The number of character Y: ', str.count('Y'))
Output:
The number of character a: 3
The number of character i: 2
The number of character y: 3
The number of character k: 0
The number of character Y: 0
Syntax
str.count(substring, start=..., end=...)
Parameters
Name | Description |
substring | A string whose count is to be found |
start | The position at which the string begins to search |
end | The position in the string where the search ends |
Return Value
It returns the number of occurrences of the substring in the given string.