In this post we’ll see how to write a Python program to count occurrences of each character or to count frequency of character in a String.
1. If you have to write the Python program to count frequency of each character without using any String method then you can write it using an outer and inner for loop. In the outer loop take the character at index 0 and in the inner loop check if such character is found again in the string, if yes then increment count.
replace()
method of the str class is used to remove all the occurrences of the character for which count is done so that same character is not picked again.
def count_char(text): for i in range(len(text)): if len(text) == 0: break; ch = text[0] # don't count frequency of spaces if ch == ' ' or ch == '\t': continue count = 1 for j in range(1, len(text)): if ch == text[j]: count += 1 # replace all other occurrences of the character # whose count is done, strip() is required for # scenario where first char is replaced and there is # space after that text = text.replace(ch, '').strip() print(ch + " - ", count) count_char('netjs blog for Python')
Output
n - 2 e - 1 t - 2 j - 1 s - 1 b - 1 l - 1 o - 3 g - 1 f - 1 r - 1 P - 1 y - 1 h - 1
2. You can use count()
method in Python which is used to count the number of occurrences of a specific substring.
def count_char(text): for i in range(len(text)): if len(text) == 0: break; ch = text[0] if ch == ' ' or ch == '\t': continue print(ch + " - ", text.count(ch)) text = text.replace(ch, '').strip() count_char('netjs java spring python')
Output
n - 3 e - 1 t - 2 j - 2 s - 2 a - 2 v - 1 p - 2 r - 1 i - 1 g - 1 y - 1 h - 1 o - 1
3. You can also use Dictionary
to count occurrences of each character in the String. Character is stored as a key in dictionary and for each character it is
checked if that character already exists as a key in dictionary or not. If it exists then increment the value associated with that key by 1, if such a key doesn’t
exist then add it to the dictionary with value as 1.
def count_char(text): count = {} for ch in text: # don't count frequency of spaces if ch == ' ' or ch == '\t': continue # If char already in dictionary increment count # otherwise add char as key and 1 as value if ch in count: count[ch] += 1 else: count[ch] = 1 for k, v in count.items(): print('Charcater {} occurs {} times'.format(k,v)) count_char('netjs java spring python')
Output
Charcater n occurs 3 times Charcater e occurs 1 times Charcater t occurs 2 times Charcater j occurs 2 times Charcater s occurs 2 times Charcater a occurs 2 times Charcater v occurs 1 times Charcater p occurs 2 times Charcater r occurs 1 times Charcater i occurs 1 times Charcater g occurs 1 times Charcater y occurs 1 times Charcater h occurs 1 times Charcater o occurs 1 times
That's all for this topic Python Program to Count Occurrences of Each Character in a String. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Programs Page
Related Topics
You may also like-