'How to create a program that counts how many times each letter appears in a text? [duplicate]
Given a string as input, I need to output how many times each letter appears in the string. I decided to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values. How to Create a program that takes a string as input and output a dictionary, which represents the letter count that should be lowercase.
Sample Input
hello
Sample Output
{'h': 1, 'e': 1, 'l': 2, 'o': 1}
Solution 1:[1]
How about something like:
text = "This is the text to analyse"
dict = {}
for char in text:
if char in dict:
dict[char] += 1
else:
dict[char] = 1
print(dict)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stephen Graham |
