Python program to check if a given string is a pangram

Examples :

  Input:  The quick brown fox jumps over the lazy dog ​​ Output:  Yes  Input:  abcdefgxyz  Output:  No 

We have already discussed the naive approach to checking pangrams in this article . Now let`s discuss a Pythonic approach to do the same.

Approach # 1: Pythonic Naive
This method uses a loop to check if each character in the string belongs to alphabet set or not.

# Python3 program for
# Check if the string is a pangram

import string

 

def ispangram ( str ):

alphabet = "abcdefghijklmnopqrstuvwxyz"

for char in alphabet:

  if char not in str . lower ():

  return False

  

return True

 
# Driver code

string = ` the quick brown fox jumps over the lazy dog`

if (ispangram (string) = = True ):

print ( "Yes" )

else :

print ( "No" )

Exit:

 Yes 

Approach # 2 Using Python Set
Convert the given string to a set and then check if the alphabets are greater or equal or not. If the rowset is greater than or equal to, print “Yes”, otherwise “No”.

# Python3 program for
# Check if the string is a pangram

import string

 

alphabet = set (string.ascii_lowercase)

 

def ispangram (string):

return set (string.lower ( )) & gt; = alphabet

  
# Driver code

string = " The quick brown fox jumps over the lazy dog ​​"

if (ispangram (string) = = True ):

  print ( "Yes" )

else :

print ( "No" )

Exit :

 Yes 

Approach # 3: An alternative to a given method
This is another method that is used uses the Python suite to determine if a string is a pangram or not. We compose a set of lowercase alphabets and a given string. If a set of a given string is subtracted from a set of alphabets, we know if the string is a pangram or not.

# Python3 program for
# Check if the string is a pangram

import string

 

alphabet = set (string.ascii_lowercase)

 

def ispangram ( str ):

return not set (alphabet) < / code> - set ( str )

 
# Driver code

string = `the quick brown fox jumps over the lazy dog`

if (ispangram (string) = = True ):

print ( "Yes" )

else :

print ( "No" )

Output:

 Yes 

Approach # 4: ASCII method
Check if each character in the string is in the ASCII lowercase range, i.e. from 96 to 122.

# Python3 program for
# Check if the string is a pangram

import itertools

import string

  

alphabet = set (string.ascii_lowercase)

 

def ispangram ( str ):

return sum ( 1 for i in set ( str ) if 96 & lt;  ord (i) & lt; = 122 ) = = 26

 
# Driver code

string = `the quick brown fox jumps over the lazy dog`

if (ispangram (string) = = True ):

print ( "Yes" )

else :

print ( "No" )

Exit :

 Yes