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.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…