
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to find Least Frequent Character in a String
When it is required to find the least frequent character in a string, ‘Counter’ is used to get the count of letters. The ‘min’ method is used to get the minimum of values in the string, i.e every letter’s count is stored along with the letter. The minimum is obtained.
Example
Below is a demonstration of the same
from collections import Counter my_str = "highland how" print ("The string is : ") print(my_str) my_result = Counter(my_str) my_result = min(my_result, key = my_result.get) print ("The minimum of all characters in the string is : ") print(my_result)
Output
The string is : highland how The minimum of all characters in the string is : a
Explanation
The required packages are imported.
A string is defined, and is displayed on the console.
The ‘Counter’ is used to get the count of every letter in the string.
This is assigned to a variable.
The ‘min’ method is used to get the minimum of values.
This is assigned to the variable again.
This is displayed as output on the console.
- Related Articles
- Python program to find Most Frequent Character in a String
- Program to find second most frequent character in C++
- Second most frequent character in a string - JavaScript
- Find the least frequent element in an array using Python
- Python program to find occurrence to each character in given string
- Java regex program to verify whether a String contains at least one alphanumeric character.
- Find Second most frequent character in array - JavaScript
- Program to find length of longest substring with character count of at least k in Python
- JavaScript Program for the Least Frequent Element in an Array
- Program to find most frequent subtree sum of a binary tree in Python
- Program to find frequency of the most frequent element in Python
- Removing nth character from a string in Python program
- Returning the second most frequent character from a string (including spaces) - JavaScript
- C Program to find maximum occurrence of character in a string
- C Program to find minimum occurrence of character in a string

Advertisements