+ 2
How to count letters in string in python
Given a string as input, you need to output how many times each letter appears in the string. You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values. Create a program to take a string as input and output a dictionary, which represents the letter count. Sample Input: hello Sample Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1} What to do after this text = input() dict = {}
4 Respuestas
+ 1
thanks guys
this is the full code
text = input()
dict = {}
#your code goes here
for i in text:
if i in dict:
dict[i] += 1
else:
dict[i] = 1
print(dict)
please upvote
+ 2
Yusuf Hashmi
Where is your attempts?
+ 1
Loop over the characters in the string.
Check if the current character exists as a key in the letters dictionary (don't call it dict!, this is a bad habit that will cause issues in your future code). If it doesn't exist add it to the dictionary with a value of 1, otherwise, increment the value of the current character by 1. Output the dictionary.
0
This is your code coach solution do it yourself if you have not concentrate on the lessons of intermediatepython then go back and give full concentration