Python | The frequency of each character in the string

Counters | Python Methods and Functions | String Variables

Method # 1: Naive method

Just iterate over the string and form a key in the dictionary of the new element or, if the element already exists, increase its value by 1.

# Python3 demo code
# frequency of each
# naive method

 
# initializing string

test_str = "GeeksforGeeks"

 
# using a naive counting method
# of each item in the line

all_freq = {}

  

for i in test_str:

  if i in all_freq:

all_freq [i] + = 1

else :

all_freq [i] = 1

 
# print result

print ( "Count of all characters in Geek sforGeeks is: "

  +   str (all_freq))

Output:

 Count of all characters in Python.Engineering is: {'r': 1,' e': 4, 'k': 2,' G' : 2, 's': 2,' f': 1, 'o': 1} 

Method # 2: Using collections.Counter ()

The most recommended method that you can use to find all occurrences is — it is a method that actually gets the frequency of all elements and can also be used to print the frequency of a single element if required.

# Python3 demo code
# frequency of each occurrence
# collection.Counter ()

from collections import Counter

 
# initialization string

test_str = "GeeksforGeeks"

 
# using collection.Counter () to get
# count of each item the line

res = Counter ( test_str)

 
# print result

print ( "Count of all characters in Python.Engineering is:"

+   str (res))

Exit :

 Count of all characters in Python.Engineering is: Counter ({'e': 4,' s': 2, 'k': 2,' G': 2, 'o' : 1, 'r': 1,' f': 1}) 

Method # 3: Using dict.get ()

The get () method is used to check for a previously occurring character in the string, if it is new, it assigns 0 as a start and adds 1 to it, otherwise adds 1 to the earlier the stored value of this element in the dictionary.

# Python3 demo code
# frequency of each
# dict.get ()

 
# initialization string

test_str = "GeeksforGeeks"

 
# using dict.get () to count
# of each item in a line

res = {}

 

for keys in test_str:

res [keys] = res.get (keys, 0 ) + 1

  
# print result

print ( "Count of all characters in Python.Engineering is:"

+   str (res))

Output:

 Count of all characters in Python.Engineering is: {'k': 2, 'e': 4,' s': 2, 'G': 2,' f': 1, 'r': 1,' o': 1} 

Method # 4: Using set () + count ()

count () is combined and with set () can also solve this problem, in this case we just iterate over the given converted string and get the number of each character in the original string and assign this value to this element, counted using count ( ) .

# Python3 demo code
# frequency of each
# set () + count ()

 
# initializing string

test_str = "GeeksforGeeks"

 
# using set ( ) + count () to get the count
# of each item in the line

res = {i: test_str.count (i) for i in set (test_str)}

 
# print result

print ( "The count of all characters in Python.Engineering is: "

  +   str (res))

Output:

 Count of all characters in Python.Engineering is: {'G': 2,' s': 2, 'k': 2,' e' : 4, 'o': 1,' r': 1, 'f': 1} 




Get Solution for free from DataCamp guru