How to find letter frequency in python data structures | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

19th May 2021, 12:33 PM
Na Ree
6 Answers
+ 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!
19th May 2021, 12:37 PM
Lothar
Lothar - avatar
+ 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)
19th May 2021, 12:38 PM
Abhay
Abhay - avatar
+ 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!!
19th May 2021, 12:56 PM
sarada lakshmi
sarada lakshmi - avatar
+ 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))
9th Aug 2021, 3:58 PM
ANDREW TSEGAYE
ANDREW TSEGAYE - avatar
0
code:: text=input() letter=input() n=len(text) per=((text.count(letter)/n)*100) print(int(per))
24th Aug 2022, 10:12 AM
Divyanshu Singh
Divyanshu Singh - avatar
0
text= input() letter= input() z= ((text.count(letter)/len(text)) * 100) print(int(z))
17th Nov 2023, 2:09 PM
TaniYa
TaniYa - avatar