Python program to check a sentence is a pangrams or not.

A pangram is a sentence that contains every letter of the alphabet at least once. For example, "The quick brown fox jumps over the lazy dog" is a famous pangram. We can check if a sentence is a pangram using Python's set() operations.

Example Input and Output

Input: string = 'abc def ghi jkl mno pqr stu vwx yz'
Output: Yes
// contains all the characters from 'a' to 'z'

Input: string = 'python program'
Output: No
// Does not contain all the characters from 'a' to 'z'

Algorithm

Here's the step-by-step approach to check if a sentence is a pangram:

Step 1: Convert the input string to lowercase
Step 2: Extract only alphabetic characters from the string
Step 3: Convert the alphabetic characters to a set to get unique letters
Step 4: Check if the set contains all 26 letters of the alphabet
Step 5: Return True if all letters are present, False otherwise

Using Set Operations

The most efficient approach uses Python's set operations to compare alphabetic characters ?

def checkPangram(sentence):
    # Convert to lowercase and create set of alphabetic characters
    alphabet_set = set(char.lower() for char in sentence if char.isalpha())
    
    # Check if all 26 letters are present
    return len(alphabet_set) == 26

# Test with example sentences
sentence1 = "The quick brown fox jumps over the lazy dog"
sentence2 = "python program"

print(f'"{sentence1}" is a pangram: {checkPangram(sentence1)}')
print(f'"{sentence2}" is a pangram: {checkPangram(sentence2)}')
"The quick brown fox jumps over the lazy dog" is a pangram: True
"python program" is a pangram: False

Using Boolean Array Method

Another approach uses a boolean array to track which letters are present ?

def checkPangramArray(sentence):
    # Create boolean array for 26 letters
    present = [False] * 26
    
    # Mark letters as present
    for char in sentence.lower():
        if char.isalpha():
            index = ord(char) - ord('a')
            present[index] = True
    
    # Check if all letters are present
    return all(present)

# Test the function
test_sentence = "Pack my box with five dozen liquor jugs"
print(f'"{test_sentence}" is a pangram: {checkPangramArray(test_sentence)}')
"Pack my box with five dozen liquor jugs" is a pangram: True

Interactive Pangram Checker

Here's a complete program that takes user input and checks for pangrams ?

def checkPangram(sentence):
    alphabet_set = set(char.lower() for char in sentence if char.isalpha())
    return len(alphabet_set) == 26

def findMissingLetters(sentence):
    alphabet_set = set(char.lower() for char in sentence if char.isalpha())
    all_letters = set('abcdefghijklmnopqrstuvwxyz')
    return sorted(all_letters - alphabet_set)

# Test with different sentences
sentences = [
    "The quick brown fox jumps over the lazy dog",
    "python programming",
    "Pack my box with five dozen liquor jugs"
]

for sentence in sentences:
    is_pangram = checkPangram(sentence)
    print(f'"{sentence}"')
    
    if is_pangram:
        print("? This is a pangram!")
    else:
        missing = findMissingLetters(sentence)
        print(f"? Not a pangram. Missing letters: {', '.join(missing)}")
    print()
"The quick brown fox jumps over the lazy dog"
? This is a pangram!

"python programming"
? Not a pangram. Missing letters: a, b, c, d, e, f, h, j, k, l, q, s, u, v, w, x, z

"Pack my box with five dozen liquor jugs"
? This is a pangram!

Comparison

Method Time Complexity Space Complexity Best For
Set Operations O(n) O(1) Simple and readable
Boolean Array O(n) O(1) Traditional approach

Conclusion

Use the set operations method for checking pangrams as it's more concise and readable. Both approaches have O(n) time complexity and efficiently determine if a sentence contains all 26 letters of the alphabet.

Updated on: 2026-03-24T20:56:08+05:30

470 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements