Python code: Find the frequency of each character in the string
- Home
- Find elements
- Python code: Find the frequency of each character in the string
- On
- By
- 0 Comment
- Categories: Find elements, String
Python code: Find the frequency of each character in the string
Python code: Find the frequency of each character in the string
In this article, we will discuss the concept of the Python code: Find the frequency of each character in the string
In this post, we are going to learn how to find the frequency of the each character of the given string in Python programming language
Program to find the frequency of the each characters in the string
find the frequency of the each character in the string -method 1
The program allows the user to enter a String and then it finds the frequency of the each characters in the given string
program 1
string=input("Enter the string ") freq=[None]*len(string) for i in range(0,len(string)): freq[i]=1 for j in range(i+1,len(string)): if(string[i]==string[j]): freq[i]=freq[i]+1 string=string[:j]+'0'+string[j+1:]; print("Character and their frequency"); for i in range(0,len(freq)): if(string[i]!=' ' and string[i]!='0'): print(string[i]+"="+str(freq[i]))
When the above code is executed, it produces the following result
Enter the string python Character and their frequency p=1 y=1 t=1 h=1 o=1 n=1
find the frequency of the each character in the string -method 2
The program allows the user to enter a String and then it finds the frequency of the each character in the given string
Program 2
string=input("Enter the string: ") str1=list(string) strlist=[] for j in str1: if j not in strlist: strlist.append(j) count=0 for i in range(len(str1)): if j==str1[i]: count+=1 print("{},{}".format(j,count))
When the above code is executed, it produces the following result
Enter the string: mypython m,1 y,2 p,1 t,1 h,1 o,1 n,1
Suggested for you
if statements in Python
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 frequebcy of a character of a string
C program to count the frequebcy of a character of a string
Python program to count the frequebcy of a character of a string
C++ program to count the frequebcy of a character of a string