Press "Enter" to skip to content

How to Count Occurrences of a Character in a String Using Python?

Here is an example to count occurrences of a character in a string using Python.

Python Program to Count Occurrences of a Character in a String

Count a character in a string using Python

def countChars(findChar, aString):
    count = 0
    for character in aString:
        if findChar == character:
            count = count + 1

    return count

print(countChars('f', 'foxinfotech'))

print(countChars('i', 'foxinfotech vinish kapoor blog'))

print(countChars('F', 'Foxinfotech'))

Output:

2
3
1

Process finished with exit code 0

See also: