Python Program to Check if a String is a Pangram or Not

1. Introduction

A pangram is a unique sentence in which every letter of the alphabet is used at least once. Pangrams are often used in typography to display typefaces or test equipment. In Python, checking if a sentence is a pangram involves comparing the set of letters in the sentence to the set of letters in the alphabet.

A pangram is a sentence that contains every letter of the alphabet at least once. The most well-known English pangram is "The quick brown fox jumps over the lazy dog".

2. Program Steps

1. Define the alphabet set.

2. Take a string input to check.

3. Normalize the string to a standard format for comparison.

4. Check if the string contains all the letters in the alphabet.

5. Print the result stating whether the string is a pangram or not.

3. Code Program

import string

# Function to check if the string is a pangram
def is_pangram(str_input):
    # Normalize the input string: remove spaces and convert to lowercase
    normalized_str = ''.join(str_input.replace(' ', '').lower())
    # Create a set of all characters in the normalized string
    char_set = set(normalized_str)
    # Create a set of all alphabet characters
    alphabet_set = set(string.ascii_lowercase)
    # Check if the character set of the string is the same as the alphabet set
    return char_set >= alphabet_set

# Input string
input_string = "The quick brown fox jumps over the lazy dog"
# Check if the input_string is a pangram
pangram_status = is_pangram(input_string)
# Print the result
print(f"Is the string a pangram? {pangram_status}")

Output:

Is the string a pangram? True

Explanation:

1. The string module's ascii_lowercase constant provides a string of all lowercase letters in the alphabet.

2. is_pangram function takes str_input as an argument, normalizes it by removing spaces and converting to lowercase, and then creates a set char_set from it.

3. alphabet_set is a set of all lowercase alphabets.

4. The function returns True if char_set contains at least all the characters in alphabet_set, indicating it is a pangram.

5. input_string is defined as "The quick brown fox jumps over the lazy dog".

6. The is_pangram function is called with input_string and the result is stored in pangram_status.

7. The result is printed out, confirming that the provided input_string is indeed a pangram as the function returns True.

Comments