String

Python program: find the frequency of a character in a string

Python program: find the frequency of a character in a string

In this article, we will discuss the concept of the Python program: find the frequency of the given character in a string

In this post, we are going to learn how to  find the frequency of  the given character in the string in Python programming language

Find the frequency of a character in a string

Find the frequency of  the given character in the string

find the frequency of  the given character in the string using for loop

The program allows the user to enter a String and then it finds the frequency of the given character in a given string using for loop in Python programing language

Program 1

#Python program to count Occurrence of a character in a string
string=input("Enter the string: ")
char=input("Please enter the char to find frequency of ta character\n")

count=0
for i in range(len(string)):
    if(string[i]==char):
        count=count+1
print("The frequency of the ",char,"in the string is: ",count)

When the above code is executed, it produces the following result

Enter the string: code for coding to coders
Please enter the char to find the frequency of the character
o
The frequency of the o in the string is: 5

 

Find the frequency of a character in a string using while loop

The program allows the user to enter a String and then it finds the frequency of the given character in a string using while loop in Python programing language

Program 2

#Python program to count Occurrence of a character in a string
string=input("Enter the string: ")
char=input("Please enter the char to find frequency of ta character\n")
count=0
i=0
while(i<len(string)):
    if(string[i]==char):
        count=count+1
    i=i+1
print("The frequency of the ",char,"in the string is: ",count)

When the above code is executed, it produces the following result

Enter the string: Python programming language
Please enter the char to find the frequency of the character
a
The frequency of the o in the string is: 3

 

Approaches

  1. Declare str and ch variables;
  2. Declare and initialize integer variables as count=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. The user asked to enter a character
  6. The given character is stored in the variable ch;
  7. A loop(for, while) is used to count frequency of the character in the given string using calculate the length of the string.
  8. Use an if condition to test if(string[i]==char), when the if statements is true,  The count becomes count + 1( count = count +1);
  9. When it is false, terminates the loop
  10. Finally, the program displays the frequency of the given character of the given string

Find the frequency of a character in a string using the function

The program allows the user to enter a String and then it finds the frequency of the given character in a string using the function in Python programing language

Program 3

#Python program to count Occurrence of a character in a string
def count_Char(ch,str1):
   count=0
   for i in range(len(string)):
     if(string[i]==char):
         count=count+1
   return count;      
string=input("Enter the string: ")
char=input("Please enter the char to find frequency of ta character\n")

Total=count_Char(char,string)
print("The frequency of the ",char,"in the string is: ",Total)

When the above code is executed, it produces the following result

Enter the string: Python program oop
Please enter the char to find the frequency of ta character
o
The frequency of the o in the string is: 4

 

Suggested for you

for loop in Python language

while loop in Python language

Function in Python language

 

Similar post

C++ program to count the total number of characters in the given string

C program to count the total number of characters in the given string

Python program to count the total number of characters in the given string

 

Java program to count the total number of  upper case lower case numeric and special character

C program to count the total number of  upper case lower case numeric and special character

Python program to count the total number of  upper case lower case numeric and special character

C++ program to count the total number of  upper case lower case numeric and special character

 

C++ program: find frequency of a character in the string
Program to count white space of the given string in Java
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago