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 ]
Program to Find the Least Frequent Character in a String in Python
Below are the ways to print the least frequent character in a string in Python.
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
Method #1: Using Counter() (Hashing , Static Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string a static input and store it in a variable.
- Calculate the frequency of all the given string elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say elementsfrequency)
- Calculate the minimum frequency character in the given string using the min() and “get” function and store it in a variable.
- Print the least frequency character in the given string by printing the above variable.
- The Exit of the Program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the string a static input and store it in a variable.
gvnstrng = 'zzzyyddddeeeee'
# Calculate the frequency of all the given string elements
# using the Counter() function which returns the element
# and its frequency as key-value pair and store this
# dictionary in a variable(say elementsfrequency)
elementsfrequency = Counter(gvnstrng)
# Calculate the minimum frequency character in the given string
# using the min() and "get" function and store it in a variable.
minfreqchar = str(min(elementsfrequency, key=elementsfrequency.get))
# Print the least frequency character in the given string
# by printing the above variable.
print('The least frequency character in the given string',
gvnstrng, 'is [', minfreqchar, ']')
Output:
The least frequency character in the given string zzzyyddddeeeee is [ y ]
Method #2: Using Counter() (Hashing , User Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string a user input using the input() function and store it in a variable.
- Calculate the frequency of all the given string elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say elementsfrequency)
- Calculate the minimum frequency character in the given string using the min() and “get” function and store it in a variable.
- Print the least frequency character in the given string by printing the above variable.
- The Exit of the Program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the string a static input and store it in a variable.
gvnstrng = input('Enter some random string = ')
# Calculate the frequency of all the given string elements
# using the Counter() function which returns the element
# and its frequency as key-value pair and store this
# dictionary in a variable(say elementsfrequency)
elementsfrequency = Counter(gvnstrng)
# Calculate the minimum frequency character in the given string
# using the min() and "get" function and store it in a variable.
minfreqchar = str(min(elementsfrequency, key=elementsfrequency.get))
# Print the least frequency character in the given string
# by printing the above variable.
print('The least frequency character in the given string',
gvnstrng, 'is [', minfreqchar, ']')
Output:
Enter some random string = btechgeeks The least frequency character in the given string btechgeeks is [ b ]
Related Programs:
- java program to find the frequency of character in a string
- python program to remove the first occurrence of character in a string
- python program to find a pair with the given sum in an array
- python program to find the second largest number in a list
- python program to count the frequency of words appearing in a string using a dictionary
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to find the sum of elements in a list recursively
