Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Sort Strings by maximum frequency character
Next article icon

Maximum Frequency Character in String – Python

Last Updated : 28 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of finding the maximum frequency character in a string involves identifying the character that appears the most number of times. For example, in the string “hello world”, the character ‘l’ appears the most frequently (3 times).

Using collection.Counter

Counter class from the collections module is an easy way to count the occurrences of each character in a string. It returns a dictionary-like object where keys are the characters and values are their frequencies.

Python
from collections import Counter  s = "hello world"  # Count the frequency of characters frequency = Counter(s)  # Get the character with the maximum frequency max_char = max(frequency, key=frequency.get)  print(max_char) 

Output
l 

Explanation

  • Counter(s) creates a dictionary where the keys are characters and the values are the counts of those characters in the string s.
  • max(frequency, key=frequency.get) finds the character with the highest frequency by comparing the values (counts) in the dictionary. The result is stored in max_char and printed.

Using dict.get() with max()

This method involves creating a dictionary to store the frequency of each character and then using the max() function to find the character with the highest frequency. The dict.get() method helps in counting occurrences efficiently.

Python
s = "hello world"  # Create a dictionary to store character frequencies freq = {}  # Count the frequency of each character for char in s:     freq[char] = freq.get(char, 0) + 1  # Get the character with the maximum frequency max_char = max(freq, key=freq.get)  print(max_char) 

Output
l 

Explanation

  • We loop through each character in the string and use freq.get(char, 0) + 1 to update the frequency of each character.
  • max(freq, key=freq.get) finds the key (character) with the highest frequency by comparing the values (counts) in the dictionary .

Using str.count()

str.count() method counts the occurrences of a specific character in the string. By iterating over each unique character and using this method, we can find the one with the maximum frequency.

Python
s = "hello world"  # Initialize variables for tracking max frequency and character max_char = '' max_count = 0  # Loop through each unique character for char in set(s):     count = s.count(char)     if count > max_count:         max_count = count         max_char = char  print(max_char) 

Output
l 

Explanation

  • We loop through each unique character in the string using set(s) to avoid duplicates.
  • s.count(char) is used to calculate the frequency of each character. If a character’s count is greater than the current max_count, we update max_count and max_char accordingly.

Using sorted() with key

sorted() function can be used to sort the characters based on their frequency. By passing a custom sorting key like s.count(char), we can sort the characters in descending order of frequency and select the first one.

Python
s = "hello world"  # Sort the string and find the character with the maximum frequency max_char = sorted(set(s), key=lambda char: s.count(char), reverse=True)[0]  print(max_char) 

Output
l 

Explanation

  • sorted(set(s), key=lambda char: s.count(char), reverse=True) sorts the unique characters from the string based on their frequency in descending order.
  • The [0] index retrieves the character with the highest frequency, which is then printed.


Next Article
Python - Sort Strings by maximum frequency character
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python - Sort Strings by maximum frequency character
    Given a string, the task is to write a Python program to perform sort by maximum occurring character. Input : test_list = ["geekforgeeks", "bettered", "for", "geeks"] Output : ['for', 'geeks', 'bettered', 'geekforgeeks'] Explanation : 1 < 2 < 3 < 4, is ordering of maximum character occurren
    3 min read
  • Python - Least Frequent Character in String
    The task is to find the least frequent character in a string, we count how many times each character appears and pick the one with the lowest count. Using collections.CounterThe most efficient way to do this is by using collections.Counter which counts character frequencies in one go and makes it ea
    3 min read
  • Python - Expand Character Frequency String
    Given a string, which characters followed by its frequency, create the appropriate string. Examples: Input : test_str = 'g7f2g3i2s2b3e4' Output : gggggggffgggiissbbbeeee Explanation : g is succeeded by 7 and repeated 7 times. Input : test_str = 'g1f1g1' Output : gfg Explanation : f is succeeded by 1
    4 min read
  • Python | Maximum Difference in String
    Sometimes, we might have a problem in which we require to get the maximum difference of 2 numbers from Strings but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Let’s discuss certain ways in which this problem can be solved. Met
    4 min read
  • Check if string contains character - Python
    We are given a string and our task is to check if it contains a specific character, this can happen when validating input or searching for a pattern. For example, if we check whether 'e' is in the string 'hello', the output will be True. Using in Operatorin operator is the easiest way to check if a
    2 min read
  • Python - Successive Characters Frequency
    Sometimes, while working with Python strings, we can have a problem in which we need to find the frequency of next character of a particular word in string. This is quite unique problem and has the potential for application in day-day programming and web development. Let's discuss certain ways in wh
    6 min read
  • Python program to equal character frequencies
    Given a String, ensure it has equal character frequencies, if not, equate by adding required characters. Input : test_str = 'geeksforgeeks' Output : geeksforgeeksggkkssfffooorrr Explanation : Maximum characters are 4 of 'e'. Other character are appended of frequency 4 - (count of chars). Input : tes
    2 min read
  • Count the number of characters in a String - Python
    The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Let’s explore different approaches to accomplish this. Using len()len() i
    3 min read
  • Python | Consecutive Character Maximum difference
    Sometimes, we might have a problem in which we require to get the maximum difference of 2 numbers from list but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Let’s discuss certain ways in which this problem can be solved. Method
    5 min read
  • Remove Multiple Characters from a String in Python
    Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Let’s explore the different ways to achieve this in det
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences