0
How to find letter frequency in python data structures
You are making a program to analyze text. Take the text as the first input and a letter as the second input, and output the frequency of that letter in the text as a whole percentage. Sample Input: hello l Sample Output: 40 The letter l appears 2 times in the text hello, which has 5 letters. So, the frequency would be (2/5)*100 = 40. Please help, thanks.
6 Antworten
+ 8
Na Ree ,
before we can help you, please show us your attempt. please put your code in playground and link it here. if you have not done a try by yourself so far, please do so.
thanks for your understanding & happy coding!
+ 3
Maintain a counter variable.
Loop over the string , see if the inputted character matches the character in string and if it do , increase the counter .
Then do (counter/len(string)*100)
+ 2
Hint:-
Use count() to get the count of the given character. Almost done!!
And about the rest, you just have to compute after getting the count.
Syntax of count() :-
string.count(char)
Ex:- String = hello
cnt = String.count('l')
print(cnt) => results in 2
Hope this helps and you can do it now!!
+ 2
Easies and fastest code to got the answer is below!!✔✔✔👍👍👍👍✔✔✔
text = input()
letter = input()
z = text.count(letter)
print(int(z / len(text)*100))
0
code::
text=input()
letter=input()
n=len(text)
per=((text.count(letter)/n)*100)
print(int(per))
0
text= input()
letter= input()
z= ((text.count(letter)/len(text)) * 100)
print(int(z))