Write a Python program to count characters frequency in a string using for loop and dictionary. In this example, the for loop iterates the whole string. We assigned all the character count to the dictionary keys. Refer to the Python string article.
string = input("Please enter the Your Own String = ")
chardict = {}
for num in string:
keys = chardict.keys()
if num in keys:
chardict[num] += 1
else:
chardict[num] = 1
print(chardict)

Using Counter from the collections module
This program counts all the characters frequency in a given string using a Counter collection. For more examples, refer to the Python Basic Programs.
from collections import Counter
string = input("Please enter the Your Own String = ")
chars = Counter(string)
print("Total Characters Frequency in this String = ")
print(chars)
Please enter the Your Own String = hello python programmers
Total Characters Frequency in this String =
Counter({'o': 3, 'r': 3, 'h': 2, 'e': 2, 'l': 2, ' ': 2, 'p': 2, 'm': 2, 'y': 1, 't': 1, 'n': 1, 'g': 1, 'a': 1, 's': 1})
Using dictionary comprehension
In the program below, we use a dictionary comprehension to count the character frequency in a string.
str = input("Enter a string: ")
freq = {ch: str.count(ch) for ch in str}
print(freq)
Enter a string: apple
{'a': 1, 'p': 2, 'l': 1, 'e': 1}
Also Read