{"id":14582,"date":"2021-10-01T10:00:08","date_gmt":"2021-10-01T04:30:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=14582"},"modified":"2021-11-22T18:33:30","modified_gmt":"2021-11-22T13:03:30","slug":"python-program-to-find-the-least-frequent-character-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-least-frequent-character-in-a-string\/","title":{"rendered":"Python Program to Find the Least Frequent Character in a String"},"content":{"rendered":"
Give the string the task is to print the least frequent character in a string in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print the least frequent character in a string in Python.<\/p>\n Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output<\/a><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Give the string the task is to print the least frequent character in a string in Python. Examples: Example1: Input: Given string =zzzyyddddeeeee Output: The least frequency character in the given string zzzyyddddeeeee is [ y ] Example2: Input: Given string =btechgeeks Output: The least frequency character in the given string btechgeeks is [ b …<\/p>\nGiven string =zzzyyddddeeeee<\/pre>\n
The least frequency character in the given string zzzyyddddeeeee is [ y ]<\/pre>\n
Given string =btechgeeks<\/pre>\n
The least frequency character in the given string btechgeeks is [ b ]<\/pre>\n
Program to Find the Least Frequent Character in a String in Python<\/h2>\n
\n
Method #1: Using Counter() (Hashing , Static Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string a static input and store it in a variable.\r\ngvnstrng = 'zzzyyddddeeeee'\r\n# Calculate the frequency of all the given string elements\r\n# using the Counter() function which returns the element\r\n# and its frequency as key-value pair and store this\r\n# dictionary in a variable(say elementsfrequency)\r\nelementsfrequency = Counter(gvnstrng)\r\n# Calculate the minimum frequency character in the given string\r\n# using the min() and \"get\" function and store it in a variable.\r\nminfreqchar = str(min(elementsfrequency, key=elementsfrequency.get))\r\n# Print the least frequency character in the given string\r\n# by printing the above variable.\r\nprint('The least frequency character in the given string',\r\n gvnstrng, 'is [', minfreqchar, ']')\r\n<\/pre>\n
The least frequency character in the given string zzzyyddddeeeee is [ y ]<\/pre>\n
Method #2: Using Counter() (Hashing , User Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string a static input and store it in a variable.\r\ngvnstrng = input('Enter some random string = ')\r\n# Calculate the frequency of all the given string elements\r\n# using the Counter() function which returns the element\r\n# and its frequency as key-value pair and store this\r\n# dictionary in a variable(say elementsfrequency)\r\nelementsfrequency = Counter(gvnstrng)\r\n# Calculate the minimum frequency character in the given string\r\n# using the min() and \"get\" function and store it in a variable.\r\nminfreqchar = str(min(elementsfrequency, key=elementsfrequency.get))\r\n# Print the least frequency character in the given string\r\n# by printing the above variable.\r\nprint('The least frequency character in the given string',\r\n gvnstrng, 'is [', minfreqchar, ']')\r\n<\/pre>\n
Enter some random string = btechgeeks\r\nThe least frequency character in the given string btechgeeks is [ b ]<\/pre>\n
\n