
- 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
Using Set() in Python Pangram Checking
In this article, we will learn how to determine whether a string is “pangram” or not in Python 3.x. Or earlier. A pangram string contains every letter in the list of English language alphabets . Let us look at the illustration below −
Provided Input: str = 'This is the python blog on Tutorial point' Desired Output: No Provided Input : str='I want to contribute to a 'dxyzwuvghlkfmq' open source project' Desired Output: Yes
By definition, a perfect pangram includes every letter of the ‘26 English alphabets’ exactly once. This tutorial doesn’t include the concept of ‘perfect pangram’.
Now Let’s look at the problem statement and set of constraints.
Problem Statement − Given a string check whether it is Pangram or not.
Constraints
- Lowercase and Uppercase are considered the same.
- No compulsion on the perfect Pangram case as discussed above.
Input: First line of input contains the test string ‘str_input ’ Output: Print 'String is a Pangram' if conditions evaluate to be true, otherwise it displays 'String is not a Pangram'.
Related Data Structure
Set() & List() Comprehension
Prerequisite
String & String Operations
Let us quickly go through the algorithm we are implementing in this problem −
Our first task is to convert the complete input string in lowercase or uppercase. Here I am using uppercase conversion using upper() method of data type “string” in Python 3.x. Or earlier.
Now with the help of (str_input) function, we can create a list of all distinct elements present in the input string.
Now we will make a new list “dist_list” which contain all distinct alphabets without any number or special character.
Now check whether the length of the dist_list is 26 or not. If the condition holds true, the input is a Pangram otherwise not.
Example
# user-defined function to check Pangram def check_pangram(input): # convert input string into uppercase str_input = str_input.upper() # convert input string into Set() # a list of distinct elements will be formed. str_input = set(str_input) # separate out alphabets from numbers and special characters # ord(ch) returns the ASCII value of the character dist_list = [ char for char in str_input if ord(char) in range(ord('a'), ord('z')+1)] if len(dist_list) == 26: return 'String is a Pangram' else: return 'String is not a Pangram' # Executable main function if __name__ == "__main__": str_input = input() print check_pangram(str_input)
Conclusion
In this article, we learnt how to figure out if a string is Pangram or not using Python 3.x. Or earlier. You can implement the same algorithm to create a pangram detector program in any other programming language.
- Related Articles
- Anagram checking in Python using collections.Counter()
- Anagram checking in Python program using collections.Counter()
- Program to check whether the sentence is pangram or not using Python
- Python program to check if the string is pangram
- Program to check given string is pangram or not in Python
- Python program to check if the given string is pangram
- Determining a pangram string in JavaScript
- Client Checking file size using HTML5
- Checking for a Leap Year using GregorianCalendar in Java
- Checking power of 2 using bitwise operations in JavaScript
- Checking for vowels in array of numbers using JavaScript
- Program to define set data structure without using library set class in Python
- Checking triangular inequality on list of lists in Python
- Checking if element exists with Python Selenium.
- Count set bits using Python List comprehension
