Python Program to Count Occurrence of Characters in String

In this tutorial we will learn how to write program in Python to count occurrence of character in a String.

Also Read this:  C program to count the occurrence of a character in String.

How this Python Program will behave?

This Python program will take a string and a character as an input. And it will count the  occurrence of a given character. This program will also count spaces I we will give it.

For example:
If we have given a String as an input “Quescol is educational website” and also a character ‘s’.

Then after counting character ‘a’, program will return output 2.

Python Program to count occurrence of given character in string

Program 1: Using for Loop

string = input("Please enter String : ")
char = input("Please enter a Character : ")
count = 0
for i in range(len(string)):
    if(string[i] == char):
        count = count + 1
print("Total Number of occurence of ", char, "is :" , count) 

Output

Please enter String : quescol
Please enter a Character : e
Total Number of occurence of  e is : 1

Program Explanation

This Python program helps you find out how many times a specific character appears in a text you enter. First, the program asks you to type any text, which could be a word or a sentence, and then it asks you to enter just one character that you want to count in that text. The program starts with a counter set to zero. It then goes through each character in the text you entered, one by one, from the beginning to the end. Each time it finds the character you’re counting, it adds one to the counter. After it checks all the characters, the program shows you a message with the total number of times your chosen character was found in the text.

Program 2: Using while Loop

string = input("Please enter String : ")
char = input("Please enter a Character : ")
index, count = 0, 0
while(index < len(string)):
    if(string[index] == char):
        count = count + 1
    index = index + 1
print("Total Number of occurence of ", char, "is :" , count) 

Output

Please enter String : quescol
Please enter a Character : e
Total Number of occurence of  e is : 1

Program Explanation

This Python program calculates how many times a specific character appears in a text you type in. The program starts by asking you to enter any text, like a word or a sentence. Next, it asks you to type just one character that you want to count in the text you just provided. The program uses two numbers: one to keep track of where it is in the text (index) and one to count how many times it finds the character (count). Both start at zero.

The program then goes through each character in your text, one by one, using a while loop, which keeps running as long as the index is less than the length of the text. If it finds the character you chose, it adds one to the count. The index increases by one each time, moving to the next character in the text. When there are no more characters left to check, the program shows a message telling you how many times your chosen character was found in the text. This method helps you easily see how often a certain letter or symbol appears in your entered text.

Program 3: Using Method

def countOccur(char, string):
    count = 0
    for i in range(len(string)):
        if(string[i] == char):
            count = count + 1
    return count
string = input("Please enter String : ")
char = input("Please enter a Character : ")
count = countOccur(char, string)
print("Total Number of occurence of ", char, "is :" , count) 

Output

Please enter String : quescol
Please enter a Character : e
Total Number of occurence of  e is : 1

Program Explanation

The above Python program helps you count how many times a specific character appears in a text you type in, using a function to do the counting. The program begins by defining a function called countOccur, which takes a character and a string as inputs. Inside this function, it sets up a counter count at zero. The function then goes through each character in the string using a loop, and each time it finds the character you’re interested in, it adds one to the counter.

After checking all the characters, the function returns the total count. Below the function, the program asks you to type any text and a character to count. It then calls the countOccur function with the character and string you entered and stores the result in a variable count. Finally, the program prints out a message telling you how many times the character appeared in the text. This way, you can easily find out the frequency of a particular letter or symbol in your text.