Check if a string is pangram in Python

In this tutorial, we will be looking at how to check if a string is a pangram or not in Python. Pangram check for a string can be done using two methods, which we will be discussing below.


A pangram is a sentence containing every letter of the alphabet. They are not really used in real life but, a code to check if each letter is present in the given sentence can come in handy. You can read more about pangram here.
A few examples:

The quick brown fox jumps over the lazy dog.



Pangram Check

Method #1: Using String Properties

In this method, we will first set a alphabet string that includes all the alphabets. Now, we use a loop to check if each char in alphabet string is in the inputted string or not. Accordingly, we print the output.

def pangram_check(str): 
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    for char in alphabet: 
        if char not in str.lower(): 
            return False
    return True
str = 'The quick brown fox jumps over the lazy dog'
if(pangram_check(str) == True): 

In the above Python program, we are passing the str¬†variable, which is our input string, to the pangram_check() function, which checks if the given string is a pangram or not. Inside the function we define a string alphabet¬†which includes all the alphabets. We start a loop and check if each character in alphabet¬†is present or not in the input. If any alphabet is not in str then we return False and print “No”. But, if the loop completes execution, i.e. each character from alphabet is present in str, then we return True and print “Yes”. We are using str.lower() to convert the string to all lowercase alphabets.

Method #2: Using Sets

This is a much smarter way as we use a property of sets called difference. You can read more about it here. We convert the input string to lowercase for ease of comparison. We then find the difference between the input string and set of all lowercase alphabets, based on the output we get to know whether the string is a pangram or not. For this subtraction we use difference.

import string

alphabet = set(string.ascii_lowercase) 
def pangram_check(str): 
     return not set(alphabet) - set(str) 
str = 'Pack my box with five dozen liquor jugs'
if(pangram_check(str.lower()) == True): 

We first import string. This is because we need this to call string.ascii_lowercase which is a set of all lowercase alphabets. We now take our input string, str, and pass it to pangram_check() after converting it to lowercase using str.lower(). The user defined function pangram_check() returns a positive integer if alphabet has some letters which are not in str and it returns 0 if all the elements of the alphabet set are present in str set. We return the not of this value as we are comparing it with True in the main code. 0 is considered False and any positive integer is True. After comparing we print”Yes” or “No” to complete the program.



Here it is, simple ways of checking if the given string is a pangram or not in Python.

Also read,

Leave a Reply

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