
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to find Most Frequent Character in a String
When it is required to find the most frequent character in a string, an empty dictionary is created, and the elements in the string are iterated over. When a character is found in the dictionary, it is increment, else it is assigned to 1. The maximum of the values in the dictionary is found, and assigned to a variable.
Example
Below is a demonstration of the same
my_string = "Python-Interpreter" print ("The string is : ") print(my_string) max_frequency = {} for i in my_string: if i in max_frequency: max_frequency[i] += 1 else: max_frequency[i] = 1 my_result = max(max_frequency, key = max_frequency.get) print ("The maximum of all characters is : ") print(my_result)
Output
The string is : Python-Interpreter The maximum of all characters in Python-Interpreter is : e
Explanation
A string is defined, and is displayed on the console.
An empty dictionary is created.
The letters in the string are iterated over, and if it has been matched to a character, it is incremented.
Else, it is assigned to 1.
The maximum of the values in the dictionary is determined.
It is assigned to a variable.
This variable is displayed as output on the console.
- Related Articles
- Python program to find Least Frequent Character in a String
- Program to find second most frequent character in C++
- Second most frequent character in a string - JavaScript
- Find Second most frequent character in array - JavaScript
- Program to find frequency of the most frequent element in Python
- Program to find most frequent subtree sum of a binary tree in Python
- Returning the second most frequent character from a string (including spaces) - JavaScript
- Find most frequent element in a list in Python
- C# program to find the most frequent element
- Finding the second most frequent character in JavaScript
- Python program for most frequent word in Strings List
- Program to find out the index of the most frequent element in a concealed array in Python
- Python Program to crawl a web page and get most frequent words
- Python program to find the most occurring character and its count
- Find the k most frequent words from data set in Python

Advertisements