Checking if a String is a Pangram in Python: A Beginner’s Guide

In this blog post, we’ll explore a fun and interesting problem in Python: checking if a string is a pangram. This tutorial is designed for beginners, so we’ll break down the code step-by-step and explain everything in simple terms.

What is a Pangram?

A pangram is a sentence that contains every letter of the alphabet at least once. The most famous example of a pangram in English is:

“The quick brown fox jumps over the lazy dog.”

This sentence uses every letter from A to Z, making it a perfect pangram.

Our Goal

We’ll write a Python program that takes a string as input and checks whether it is a pangram. We’ll do this without using any complex built-in functions, making it a great exercise for beginners to understand loops, sets, and basic string manipulation.

Step-by-Step Solution

Here’s how we’ll approach the problem:

  1. Take Input from the User: We’ll first ask the user to enter a string.
  2. Create a Set of All Letters: We’ll create a set containing all the letters of the alphabet.
  3. Filter the Input String: We’ll go through the input string, converting it to lowercase, and add only the alphabetic characters to another set.
  4. Compare the Sets: We’ll subtract the set of characters from the input string from the set of all letters. If the result is an empty set, then the input string is a pangram.
  5. Print the Result: Finally, we’ll print whether the string is a pangram or not.

The Code

Here’s the Python code that accomplishes this:

def is_pangram(input_string):
    # Step 2: Create a set of all lowercase letters
    all_letters = set("abcdefghijklmnopqrstuvwxyz")

    # Step 3: Create a set of the letters in the input string
    input_string_set = set()
    for char in input_string.lower():
        if char.isalpha():  # Only consider alphabetic characters
            input_string_set.add(char)

    # Step 4: Subtract the sets to find out if any letters are missing
    missing_letters = all_letters - input_string_set

    # Step 5: Check if the result is an empty set
    if not missing_letters:
        print("pangram")
    else:
        print("not a pangram")

# Step 1: Take input from the user
user_input = input("Enter a string: ")
is_pangram(user_input)

How the Code Works

  1. Set of All Letters:
  • all_letters = set("abcdefghijklmnopqrstuvwxyz") creates a set containing all the letters from a to z.
  1. Filtering the Input String:
  • We loop through each character in the input string (converted to lowercase) and check if it’s an alphabetic character using char.isalpha().
  • If the character is a letter, we add it to input_string_set.
  1. Subtracting the Sets:
  • missing_letters = all_letters - input_string_set subtracts the set of letters found in the input string from the set of all letters.
  • If the result is an empty set, the string is a pangram; otherwise, it’s not.
  1. Output:
  • If missing_letters is empty, the code prints "pangram". If not, it prints "not a pangram".

Test Cases

Let’s test our program with some example inputs:

  1. Example 1:
  • Input: "The quick brown fox jumps over the lazy dog"
  • Output: "pangram"
  • Explanation: This classic sentence contains every letter from A to Z.
  1. Example 2:
  • Input: "Hello, World!"
  • Output: "not a pangram"
  • Explanation: The sentence is missing many letters, so it’s not a pangram.
  1. Example 3:
  • Input: "Pack my box with five dozen liquor jugs"
  • Output: "pangram"
  • Explanation: This sentence also contains every letter from A to Z.
  1. Example 4:
  • Input: "Python programming is fun!"
  • Output: "not a pangram"
  • Explanation: The sentence is missing several letters, so it’s not a pangram.

Conclusion

By following the steps in this guide, you’ve created a Python program that can accurately determine whether a string is a pangram. This exercise helps reinforce your understanding of loops, sets, and string manipulation in Python. Keep practicing with different strings to deepen your knowledge and improve your programming skills!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top