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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago