Program to find the most frequently used character in the string using Python

Introduction

In our previous section of python programming, we understood the code to find the least frequent character in the given string. Now we will learn how to find the most common character in the string.

Program to find the most frequently used character in the string

Program

from collections import Counter




input_str = input("Enter the string : ")

input_str = input_str.lower()

# Using Counter() and max()

output = Counter(input_str)

output = max(output, key = output.get)




print("The most frequently used character in the given string: "+str(output))

Output

Program to find the most frequently used character in the string Output

Explanation

In the above code we have used Counter() to get the frequency of each character present in the given string and stored it to variable output. To find the maximum of all, we applied max function to the output variable and stored the result in that variable only. The resultant character is printed using the print function.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.