# Character Frequency in a Python String

This tutorial provides several techniques to compute the frequency of each character in a Python string, followed by simple examples.

Here, we have to write a program that will take an input string and count the occurrence of each character in it. We can solve this problem with different programming logic. Let’s check out each solution one by one.

## Python Program – Compute Frequency of Characters in a String

It is always interesting to address a problem by taking different approaches. A real programmer keeps trying and continues thinking of doing things in a better way.

### Novice approach – Use A dictionary for the frequency of chars

It is the simplest way to count the character frequency in a Python string. You can take a dictionary, use its keys to keep the char, and the corresponding values for the no. of occurrences. It just requires to keep incrementing each value field by 1.

See the full logic in the below coding snippet.

```"""
Python Program:
Using a dictionary to store the char frequency in string
"""
input_string = "Data Science"
frequencies = {}

for char in input_string:
if char in frequencies:
frequencies[char] += 1
else:
frequencies[char] = 1

# Show Output
print ("Per char frequency in '{}' is :n {}".format(input_string, str(frequencies)))```

The result of the above coding snippet is as follows:

```Per char frequency in 'Data Science' is :
{'D': 1, 'a': 2, 't': 1, ' ': 1, 'S': 1, 'c': 2, 'i': 1, 'e': 2, 'n': 1}```

### Using collections.Counter() to print character frequency

Subsequently, you can use Python’s collection module exposes counter() function. It computes the character frequency and returns a dictionary-like object.

See the full logic in the below coding snippet.

```"""
Python Program:
Using collections.Counter() to print the char frequency in string
"""
from collections import Counter

input_string = "Data Science"

frequency_per_char = Counter(input_string)

# Show Output
print ("Per char frequency in '{}' is :n {}".format(input_string, str(frequency_per_char)))
print ("Type of frequency_per_char is: ", type(frequency_per_char))```

The result of the above coding snippet is as follows:

```Per char frequency in 'Data Science' is :
Counter({'a': 2, 'c': 2, 'e': 2, 'D': 1, 't': 1, ' ': 1, 'S': 1, 'i': 1, 'n': 1})
Type of frequency_per_char is:  <class 'collections.Counter'>```

### Dictionary’s get() method to compute char frequency

We’ve seen two approaches so far in this tutorial.  However, we can further fine-tune our previous logic of using a Python dictionary and use its get() function instead. This method allows us to set the key-value to zero for a new char or increment by one otherwise.

See the full logic in the below coding snippet.

```"""
Python Program:
Using dict.get() to print the char frequency in string
"""
input_string = "Data Science"

frequency_table = {}

for char in input_string:
frequency_table[char] = frequency_table.get(char, 0) + 1

# Show Output
print ("Character frequency table for '{}' is :n {}".format(input_string, str(frequency_table)))```

After executing the above code, you see the following result:

```Character frequency table for 'Data Science' is :
{'D': 1, 'a': 2, 't': 1, ' ': 1, 'S': 1, 'c': 2, 'i': 1, 'e': 2, 'n': 1}```

### Python set() method to compute character frequency

With the help of Python’s set() method, we can accomplish this assignment. Besides, we’ll need to make use of the count() function to keep track of the occurrence of each character in the string.

See the full logic in the below coding snippet.

```"""
Python Program:
Using dict.get() to print the char frequency in string
"""
input_string = "Data Science"

frequency_table = {char : input_string.count(char) for char in set(input_string)}

# Show Output
print ("Character frequency table for '{}' is :n {}".format(input_string, str(frequency_table)))
print ("Type of 'frequency_table' is: ", type(frequency_table))```

After executing the above code, you see the following result:

```Character frequency table for 'Data Science' is :
{'D': 1, 'i': 1, 'n': 1, ' ': 1, 't': 1, 'S': 1, 'e': 2, 'c': 2, 'a': 2}
Type of 'frequency_table' is:  <class 'dict'>```

Please note that the output of the above approach is also a dictionary object. To learn more, read our flagship Python tutorial for beginners and advanced learners.