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.
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))
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.