Linux, OpenSource, Programming And Hacks

Jul 20, 2017

Python Program to find frequency of Characters in a String

Jul 20, 2017
We will find the count of repeating characters in a string using python3.

Here we have used dictionary data structure to make a key-value pair for all alphabets and numbers, where the key is the alphabet or number and value is its count.

so this is a kind of hashing algorithm as we are mapping keys and values.

first we initialize all counts to 0

and on scanning the string we increment the count on getting the same character again.

s = input("Enter a string: ") #use raw_input for python2

alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
d1 = {letter: 0 for letter in alphabets} #initializing all alphabets, numbers to count 0

for ch in s:
 d1[ch] += 1 #incrementing count on repeat

for key in d1:

You can take this Python Quiz

No comments:

Post a Comment