Python Program to Check if a String Is a Pangram
Beginner · Strings
A pangram is a sentence that uses every letter of the alphabet at least once, like "The quick brown fox jumps over the lazy dog". This program checks each letter from a to z and lists any that are missing.
Program
Sentence: The quick brown fox jumps over the lazy dog It is a pangram
How it works
string.ascii_lowercaseis the string of the 26 small letters, from a to z.- The sentence is lower-cased once, so capital letters count as well.
- Every letter that does not appear in the sentence is added to
missing. - If no letters are missing, the sentence is a pangram; otherwise the program shows which letters it lacks.
Other ways to write it
Using sets
Pangram: The quick brown fox jumps over the lazy dog Not a pangram: Hello world | missing: abcfgijkmnpqstuvxyz Pangram: Pack my box with five dozen liquor jugs
alphabet <= letters asks whether every letter of the alphabet is in the sentence. alphabet - letters is the set of missing letters, sorted so they print in order.
Using all()
Pangram: Sphinx of black quartz, judge my vow Not a pangram: Python is fun
all() is True only when the check passes for every letter, and it stops as soon as one letter is missing.
Try it yourself
Change the program to also print how many different letters the sentence uses.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
- Check if Two Strings Are Anagrams
- Count Each Vowel in a String
- Remove Duplicate Characters from a String
- Count Vowels, Consonants, Uppercase and Lowercase Letters
- Check a Palindrome String
- Reverse a String
- Generate a Random Password
- Count the Frequency of Each Character in a String
- Count the Words in a String
All Python programs · CBSE class 12 practical file · Python exercises