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 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
- Declare str and ch variables;
- Declare and initialize integer variables as count=0;
- The user asked to enter a string
- The given string is stored in the variable str;
- The user asked to enter a character
- The given character is stored in the variable ch;
- A loop(for, while) is used to count frequency of the character in the given string using calculate the length of the string.
- Use an if condition to test if(string[i]==char), when the if statements is true, The count becomes count + 1( count = count +1);
- When it is false, terminates the loop
- 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
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