Count the number of occurrences of a character in a string

You can use the .count() method to count the number of occurrences of a character in a string. For example, to count the number of occurrences of the letter "a" in the string "banana", you would use the following code:

string = "banana"
count = string.count("a")
print(count)

The output would be 3, as the letter "a" appears 3 times in the string "banana".

You can also use python built-in collections library collections.Counter() to count the number of occurrences of each character in a string.

from collections import Counter
string = "banana"
count = Counter(string)
print(count)

The output would be Counter({'a': 3, 'b': 1, 'n': 2})