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

  • Post author:
  • Post category:Python
  • Reading time:1 mins read

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'))



Process finished with exit code 0

See also:

Vinish Kapoor

An Oracle Apex Consultant, Oracle ACE, and founder of and a question and answer forum for developers.