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
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
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
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
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# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…