Letter Count | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Letter Count

I need to input a word and then tell how many times each letter appear. For example, "hello", If I use ".count(Each digit in the string)," it would tell me how many times each digit in the string appear, not how many times the letter appears. Then I would need to store each letter-count in a dictionary, but I would try to figure that out later, if not, I will ask. But I just need to understand the 1st part first. Thank you for your help :)

24th Jun 2021, 3:13 AM
Robert S. Soto
Robert S. Soto - avatar
6 Answers
+ 2
Robert S. Soto You can use count() function to count number of a character appearing in a input text. EX: print("Hi".count('H')) >>>1 And you can use loops to store a character as a key and count of that as a value in dict. text = input() print({i:text.count(i) for i in text })
24th Jun 2021, 3:41 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 5
string = input() or "mississippi" counts = {char: string.count(char) for char in set(string)} print(counts) https://code.sololearn.com/c5SZz6Ecsnqz
24th Jun 2021, 6:45 AM
David Ashton
David Ashton - avatar
+ 1
Check this out, may be it can help you: https://code.sololearn.com/c8YZr2UCNvnG/?ref=app
24th Jun 2021, 4:46 AM
Shadoff
Shadoff - avatar
0
string = "hello" dictionary = dict() for i in string: if(i in dictionary.keys()): continue else: dictionary[i] = string.count(i) print(dictionary) does this help you ?
24th Jun 2021, 3:41 AM
Aravind Shetty
Aravind Shetty - avatar
0
Thank you ˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜, I could solve it more specifically with your code suggestion, and Thanks in abundance to all who answered, I truly appreciate your help.
25th Jun 2021, 12:32 AM
Robert S. Soto
Robert S. Soto - avatar
0
There are two ways you can solve this problem The first is text = input() dict= { } #loop through the text for I in text: '''then create a conditional statement that if "I" is not found on the dict add one to the dict''' if I not in dict: dict[i]=1 else: dict[i]+=1 print(dict) or text = input() dict= { } #loop through the text for I in text: for I in text: dict[i]=dict.get(i, 0)+1 print(dict)
4th May 2022, 12:39 PM
Godspower Iheanacho
Godspower Iheanacho - avatar