Maximum Frequency Character in String - Python Last Updated : 28 Feb, 2025 Summarize Comments Improve Suggest changes Share 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.CounterCounter 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) Outputl ExplanationCounter(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) Outputl ExplanationWe 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) Outputl ExplanationWe 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 keysorted() 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) Outputl Explanationsorted(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. Comment More infoAdvertise with us Next Article Python - Maximum of String Integer list M manjeet_04 Follow 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 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() is 2 min read Get Last N characters of a string - Python We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python.Using String Slicing String slicing is the fastest and most straigh 2 min read Python - Maximum of String Integer list Sometimes, while working with data, we can have a problem in which we receive a series of lists with data in string format, which we wish to find the max of each string list integer. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + int() This is the brute forc 4 min read Python | Longest Run of given Character in String Sometimes, while working with Strings, we can have a problem in which we need to perform the extraction of length of longest consecution of certain letter. This can have application in web development and competitive programming. Lets discuss certain ways in which this task can be performed. Method 6 min read Like