Change language

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 ( ))" = 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) " = 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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically