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 :)
6 Respostas
+ 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 })
+ 5
string = input() or "mississippi"
counts = {char: string.count(char) for char in set(string)}
print(counts)
https://code.sololearn.com/c5SZz6Ecsnqz
+ 1
Check this out, may be it can help you:
https://code.sololearn.com/c8YZr2UCNvnG/?ref=app
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 ?
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.
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)