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


Sample String : “google.com”
Expected Result: {'g': 2, 'o': 3, 1': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}

Answer =

 

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('Pathwala'))


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post