0
0
PythonProgramBeginner · 2 min read

Python Program to Check if String is Pangram

You can check if a string is a pangram in Python by verifying if set('abcdefghijklmnopqrstuvwxyz') is a subset of set(string.lower()), like this: is_pangram = set('abcdefghijklmnopqrstuvwxyz') <= set(input_string.lower()).
📋

Examples

InputThe quick brown fox jumps over the lazy dog
OutputTrue
InputHello World
OutputFalse
InputPack my box with five dozen liquor jugs
OutputTrue
🧠

How to Think About It

To check if a string is a pangram, think about whether it contains every letter from 'a' to 'z' at least once. Convert the string to lowercase to ignore case differences, then check if all letters in the alphabet appear in the string.
📐

Algorithm

1
Get the input string.
2
Convert the string to lowercase.
3
Create a set of all letters in the string.
4
Create a set of all letters in the English alphabet.
5
Check if the alphabet set is a subset of the string's letter set.
6
Return True if it is, otherwise False.
💻

Code

python
def is_pangram(s: str) -> bool:
    alphabet = set('abcdefghijklmnopqrstuvwxyz')
    return alphabet <= set(s.lower())

# Example usage
input_string = "The quick brown fox jumps over the lazy dog"
print(is_pangram(input_string))
Output
True
🔍

Dry Run

Let's trace the example 'The quick brown fox jumps over the lazy dog' through the code.

1

Convert string to lowercase

'the quick brown fox jumps over the lazy dog'

2

Create set of letters in string

{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}

3

Create alphabet set

{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}

4

Check if alphabet is subset of string letters

True

StepString Letters SetAlphabet SetIs Subset?
1{'a', 'b', 'c', ..., 'z'}{'a', 'b', 'c', ..., 'z'}True
💡

Why This Works

Step 1: Lowercase Conversion

Converting the string to lowercase with s.lower() ensures that letter case does not affect the check.

Step 2: Set Creation

Creating a set of letters from the string removes duplicates and allows easy membership testing.

Step 3: Subset Check

Using alphabet <= set(s.lower()) checks if all alphabet letters are present in the string.

🔄

Alternative Approaches

Using all() with a loop
python
def is_pangram(s: str) -> bool:
    s = s.lower()
    return all(chr(c) in s for c in range(ord('a'), ord('z') + 1))

print(is_pangram("The quick brown fox jumps over the lazy dog"))
This method checks each letter individually and returns False immediately if any letter is missing, which can be faster for strings missing letters early.
Using string module and set difference
python
import string

def is_pangram(s: str) -> bool:
    return not set(string.ascii_lowercase) - set(s.lower())

print(is_pangram("The quick brown fox jumps over the lazy dog"))
This uses set difference to find missing letters; if none are missing, the string is a pangram.

Complexity: O(n) time, O(1) space

Time Complexity

The program scans the string once to create a set of letters, which takes O(n) time where n is the string length.

Space Complexity

The extra space is O(1) because the alphabet set size is fixed (26 letters), and the string letter set is at most 26 letters.

Which Approach is Fastest?

Using set operations is generally faster and cleaner than looping with all(), but all() can short-circuit early if letters are missing.

ApproachTimeSpaceBest For
Set subset checkO(n)O(1)Clean and fast for most cases
all() with loopO(n)O(1)Early exit if missing letters
Set differenceO(n)O(1)Clear logic using set math
💡
Always convert the string to lowercase before checking letters to avoid case mismatches.
⚠️
Forgetting to convert the string to lowercase causes the check to fail for uppercase letters.