Python Program for Pangram

pangram python program
Reading Time: 6 minutes

Introduction:

The given Python program checks whether a given sentence is a pangram or not. A pangram is a sentence that contains every letter of the alphabet at least once. The program takes user input, converts it to lowercase, and then analyses the sentence to determine if it includes all the letters of the alphabet.

Algorithm:

  1. Define a function called is_pangram that takes a sentence as input.
  2. Inside the function: a. Convert the sentence to lowercase using the lower() method to make the comparison case-insensitive. b. Create a set named letters that stores all unique characters present in the sentence. c. Use the filter() function with a lambda function to remove any non-alphabetic characters from the letters This step ensures that only alphabet letters are considered in the pangram check. d. Compare the letters set with the set of all lowercase alphabet letters using the issubset() method. The string.ascii_lowercase constant from the string module provides all lowercase letters of the alphabet. e. Return True if the letters set contains all the lowercase alphabet letters, indicating that the sentence is a pangram. Otherwise, return False.
  3. After defining the is_pangram function, the program then tests the function by taking input from the user and displaying whether the input sentence is a pangram or not.

Code :

    
     import string
def is_pangram(sentence):
    # Convert the sentence to lowercase to make the comparison case-insensitive
    sentence = sentence.lower()
    # Create a set of all the letters in the sentence
    letters = set(sentence)
    # Remove any non-alphabetic characters from the set
    letters = set(filter(lambda char: char.isalpha(), letters))
    # Compare the set of letters with the set of all lowercase alphabet letters
    return set(string.ascii_lowercase).issubset(letters)
# Test the function
if __name__ == "__main__":
    input_sentence = input("Enter a sentence: ")
    if is_pangram(input_sentence):
        print("The sentence is a pangram.")
    else:
        print("The sentence is not a pangram.")

    
   

Input :

    
     Enter a sentence: Quick brown foxes jump over the lazy systech group's coding classes
    
   

Output :

    
     The sentence is a pangram.
    
   

Input :

    
     Enter a sentence: Jack and Jill visited Systech Group to learn Python, Java, and Django
    
   

Output :

    
     The sentence is not a pangram.
    
   

Explanation:

  1. The is_pangram function takes a sentence as input and follows the steps below to determine if it is a pangram: a. It converts the sentence to lowercase to ensure case-insensitivity. b. A set named letters is created to store all unique characters present in the sentence. c. The filter() function with a lambda function is used to filter out any non-alphabetic characters from the letters set, leaving only alphabet letters for the pangram check. d. The function then checks whether the letters set is a subset of the set containing all lowercase alphabet letters (ascii_lowercase). If the letters set contains all the lowercase alphabet letters, the function returns True, indicating that the sentence is a pangram. Otherwise, it returns False.
  2. The program then runs a test of the is_pangram function by taking input from the user using input(). The input sentence is stored in the variable input_sentence.
  3. Finally, the program prints the result of the pangram check. If the is_pangram function returns True, the program prints “The sentence is a pangram.” Otherwise, it prints “The sentence is not a pangram.” The result is displayed to the user, indicating whether the input sentence is a pangram or not.