Find all occurrences of each character in a string python

Given a string, the task is to find the frequencies of all the characters in that string and return a dictionary with key as the character and its value as its frequency in the given string.

Method #1 : Naive method

Simply iterate through the string and form a key in dictionary of newly occurred element or if element is already occurred, increase its value by 1.

test_str = "GeeksforGeeks"

all_freq = {}

for i in test_str:

    if i in all_freq:

        all_freq[i] += 1

    else:

        all_freq[i] = 1

print ["Count of all characters in GeeksforGeeks is :\n "

                                        +  str[all_freq]]

Output :

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

Method #2 : Using collections.Counter[]

The most suggested method that could be used to find all occurrences is this method, this actually gets all element frequency and could also be used to print single element frequency if required.

from collections import Counter

test_str = "GeeksforGeeks"

res = Counter[test_str]

print ["Count of all characters in GeeksforGeeks is :\n "

                                           +  str[res]]

Output :

Count of all characters in GeeksforGeeks is : 
Counter[{'e': 4, 's': 2, 'k': 2, 'G': 2, 'o': 1, 'r': 1, 'f': 1}]

Method #3 : Using dict.get[]

get[] method is used to check the previously occurring character in string, if its new, it assigns 0 as initial and appends 1 to it, else appends 1 to previously holded value of that element in dictionary.

test_str = "GeeksforGeeks"

res = {}

for keys in test_str:

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

print ["Count of all characters in GeeksforGeeks is : \n"

                                             +  str[res]]

Output :

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

Method #4 : Using set[] + count[]

count[] coupled with set[] can also achieve this task, in this we just iterate over the set converted string and get the count of each character in original string and assign that element with that value counted using count[].

test_str = "GeeksforGeeks"

res = {i : test_str.count[i] for i in set[test_str]}

print ["The count of all characters in GeeksforGeeks is :\n "

                                               +  str[res]]

Output :

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

How do you find all occurrences of a character in a string in Python?

Use the string. count[] Function to Find All Occurrences of a Substring in a String in Python. The string. count[] is an in-built function in Python that returns the quantity or number of occurrences of a substring in a given particular string.

How do you find the occurrence of all characters in a string?

In order to find occurence of each character in a string we can use Map utility of Java.In Map a key could not be duplicate so make each character of string as key of Map and provide initial value corresponding to each key as 1 if this character does not inserted in map before.

How do you count the number of occurrences of a character in a string in Python?

Python String count[] The count[] method returns the number of occurrences of a substring in the given string.

How do you find the occurrences of a word in a string in Python?

Split the string into a list containing the words by using split function [i.e. string. split[]] in python with delimiter space. Use set[] method to remove a duplicate and to give a set of unique words. Iterate over the set and use count function [i.e. string.

Chủ Đề