Counting number of occurrences of a character in a Python String
In this tutorial, we will learn how to count the number of occurrences of a character in a string in Python.
Using count() function
To count the number of occurrences of a character in a string, we can use the built-in count()
function in Python.
Here is an example, that counts the number of o
characters in the following string.
str = 'Learn online from anywhere in the world'
print(str.count('o'))
Output:
3