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:

Vinish Kapoor

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *