Python - Least Frequent Character in String Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 easy to find the least frequent character. Python from collections import Counter s = "GeeksforGeeks" freq = Counter(s) res = min(freq, key=freq.get) print(str(res)) Outputf Explanation:Counter(s): This creates a dictionary where each character in the string is a key, and the value is its frequency.min(freq, key=freq.get): This finds the character with the lowest frequency.Table of ContentUsing dictUsing SortingUsing str.count()Using dictUsing a dictionary, we count the frequency of each character in a single pass and directly find the least frequent character. This approach avoids re-scanning the string making it more efficient than naive methods. Python s = "GeeksforGeeks" d = {} # Initialize an empty dictionary for char in s: # Loop through each character in `s` d[char] = d.get(char, 0) + 1 res = min(d, key=d.get) print(str(res)) Outputf Explanation:d.get(char, 0): This retrieves the current frequency of char, defaulting to 0 if the character is not found.min(d, key=d.get): This finds the character with the lowest frequency in the dictionary d.Using SortingUsing sorting, we first count the frequency of each character in the string and then sort the characters by their frequency in ascending order. The least frequent character is extracted from the first element of the sorted list. Python s = "GeeksforGeeks" # Creating an empty dictionary d = {} # Counting the frequency of each character in the string for char in s: d[char] = d.get(char, 0) + 1 # Sorting `d` items by frequency sorted_freq = sorted(d.items(), key=lambda item: item[1]) # Extracting the least frequent character res = sorted_freq[0][0] print(res) Outputf Explanation:d[char] = d.get(char, 0) + 1: This checks if the character already exists in the dictionary d . If it does it increments its count, otherwise it initializes the count to 1.sorted(d.items(), key=lambda item: item[1]): This sorts the items in the dictionary by the frequency in ascending order.sorted_freq[0][0]: After sorting, the least frequent character is the first element in the sorted listsorted_freqand its character is extracted using [0][0].Using str.count()count()function counts the occurrences of each character and min() is used to identify the character with the lowest frequency. This approach is straightforward but becomes inefficient for larger strings due to its quadratic time complexity. Python s = "GeeksforGeeks" res = min(s, key=lambda char: s.count(char)) print(str(res)) Outputf Explanation:min(s, key=...): This finds the character with the minimum frequency in the string s .lambda char: s.count(char): For each character this lambda function calculates the frequency of that character in the string using s.count(char). Comment More infoAdvertise with us Next Article Python - Least Frequent Character in String M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Maximum Frequency Character in String - Python 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 modul 3 min read Specific Characters Frequency in String List-Python The task of counting the frequency of specific characters in a string list in Python involves determining how often certain characters appear across all strings in a given list.For example, given a string list ["geeksforgeeks"] and a target list ['e', 'g'], the output would count how many times 'e' 4 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 | 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 Python | Lowercase first character of String The problem of capitalizing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the first character of the string to lowercase. Let us discuss certain ways in which this can be performed. Method #1: Using string sli 4 min read Like