Menu Close

Frequency of Characters in a String in Python

Frequency of characters in a string in Python

In this article, we are going to find the frequency of characters in a string in Python. Here we will see various ways to find the frequency of characters in a string in Python. This is most asked by interviews when you are going to interview for Python developer Position.

Before going through this article, you should have basic knowledge of the following Python topics, because throughout this article we are going to use all these.

We have various methods to write a Python program to find the frequency of the characters in a string.
let’s understand all, one by one.

Frequency of characters in a string Python.

Using Dictioanry

We will use a Python dictionary to find the frequency of characters in a string in Python. we will follow the following process to getting the occurrence of characters in a string.

  • Create empty string.
  • Use Python for loop to iterate given string.
  • check whether each character present in the empty dictionary or not using if statement.
  • If if statement return True,increament the value by 1 otherwise create new item with key character and value 1.
  • After iteration completed of the string, you will get a dictionary with key and their total occurrence in a string.

Now, let’s take an example to understand.

Example: Frequency of character in a string using a dictionary.


string = "Programming Funda"
result = {}

for i in string:
    if i in result:
        result[i] += 1
    else:
        result[i] = 1

print("Result is:- ", result)

When you run the above program, the output will be:


Result is:-  {'P': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 2, 'm': 2, 'i': 1, 'n': 2, ' ': 1, 'F': 1, 'u': 1, 'd': 1}

Using dict.get()

We will use the Python dictionary get method to find the occurrence of the character. we will follow the following process to getting the occurrence of characters in a string.

  • Create empty string.
  • Use Python for loop to iterate each character of given string.
  • Now, we will try to get the value of key ( charcter comes from for loop ). If the key exist then increment the value by 1 oterwise return 0.

Now, let’s take an example to understand.

Example: Frequency of character in a string using dict.get()

string = "Programming Funda"

# empty dictionary
result = {}

for i in string:
    result[i] = result.get(i, 0) + 1

print("Result is:- ", result)

When you run the above program, the output will be:


Result is:-  {'P': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 2, 'm': 2, 'i': 1, 'n': 2, ' ': 1, 'F': 1, 'u': 1, 'd': 1}

Using collections.Counter()

Here we are going to use Python built-in Collections module to get the character’s frequency in the string using Python. To use the Python collections module, we need to import it by using the import keyword.

Example: Frequency of character in a string using collections.Counter()


from collections import Counter
string = "Programming"

# count the character occurrence 
result = Counter(string)

print("Result is:- ", result)

Output:


Result is:-  Counter({'r': 2, 'g': 2, 'm': 2, 'P': 1, 'o': 1, 'a': 1, 'i': 1, 'n': 1})

Using dictionary and set

Here we are going to Python dictionary comprehension and set to find the frequency of characters.
We will follow the following process.

  • define string to find character frequency
  • Inside dictionary comprehension, we convert the whole string to set and iterate each character using for loop and then count total number of occurrence of character in the string using Python string count() function.

Example: Frequency of character in a string using dict and set()


string = "Programming"

# dictionary comprehension
result = {i: string.count(i) for i in set(string)}

# result
print("Result is:- ", result)

Output

Result is:-  {'m': 2, 'n': 1, 'i': 1, 'g': 2, 'r': 2, 'o': 1, 'a': 1, 'P': 1}

Conclusion

So here, we have seen all about how to find the frequency of characters in a string in Python.
This type of question is mostly asked by interviews for Python developer positions to test your Python skills and thinking skills.

If you like this article, please share and keep visit for python’s interesting examples.

Thanks for reading….

How To Convert Datetime to String in Python
Reverse a List in Python

Related Posts