Saturday, 28 December 2024

Day 63 : Python Program to Check If a String is Pangram or Not

 


import string

def is_pangram(sentence):

    """

    Check if a sentence is a pangram.

    A pangram contains every letter of the English alphabet at least once.

    Args:

        sentence (str): The input string to check.

    Returns:

        bool: True if the sentence is a pangram, False otherwise.

    """

    alphabet = set(string.ascii_lowercase)

    letters_in_sentence = set(char.lower() for char in sentence if char.isalpha())

    return alphabet <= letters_in_sentence

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.")

#source code --> clcoding.com 

Code Explanation:

1. Importing the string module
import string
The string module provides constants such as string.ascii_lowercase, which is a string containing all lowercase letters ('abcdefghijklmnopqrstuvwxyz').

2. Function: is_pangram
def is_pangram(sentence):
    """
    Check if a sentence is a pangram.
    A pangram contains every letter of the English alphabet at least once.

    Args:
        sentence (str): The input string to check.

    Returns:
        bool: True if the sentence is a pangram, False otherwise.
    """
    alphabet = set(string.ascii_lowercase)  # Create a set of all 26 lowercase letters
alphabet: A set containing all 26 letters of the English alphabet. Using set ensures easy comparison with the letters in the sentence.

    letters_in_sentence = set(char.lower() for char in sentence if char.isalpha())
letters_in_sentence: A set comprehension that:
Iterates over each character in the sentence.

Converts the character to lowercase using char.lower() to make the comparison case-insensitive.
Checks if the character is alphabetic (char.isalpha()) to filter out non-letter characters.
Adds valid characters to the set, automatically eliminating duplicates.

    return alphabet <= letters_in_sentence
alphabet <= letters_in_sentence: This checks if all elements of the alphabet set are present in letters_in_sentence.
<=: Subset operator. If alphabet is a subset of letters_in_sentence, the function returns True.

3. Main Execution Block
if __name__ == "__main__":
    input_sentence = input("Enter a sentence: ")
This ensures that the code runs only when executed directly (not when imported as a module).
    if is_pangram(input_sentence):
        print("The sentence is a pangram.")
    else:
        print("The sentence is not a pangram.")
Takes user input and checks if it is a pangram using the is_pangram function. Prints the result accordingly.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (68) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (943) Python Coding Challenge (388) Python Quiz (40) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses