w3resource logo


Python Exercises

Python: Write a Python program to count the number of characters (character frequency) in a string


Write a Python program to count the number of characters (character frequency) in a string.
Sample String : google.com'
Expected Result : {'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1}

Sample Solution :

Python Code :

def char_frequency(str1):
    dict = {}
    for n in str1:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('google.com'))

Console :

Copy and paste the above code and press "Enter key" to execute :

Post your code through Disqus