0
19kviews
Write a Python program to count the number of characters (character frequency) in a string.
1 Answer
0
7.2kviews

Explanation:-

  • Take a string as a input from the user.
  • Create a empty dictionary.
  • Iterate over the character in the string check if the char is the present in the dictionary if yes then increment the value of the corresponding key if not then create a key.
  • Print the dictionary where and key represents the string & values represent the count of occurrence particular char in the string.

Code:-

n=input("Enter the String: ").lower()
s={}
for i in n:
    if i in s:
        s[i]+=1
    else:
        s[i]=1
print(s)

Output:-

enter image description here

enter image description here

Please log in to add an answer.