Online Tutorials & Training Materials | STechies.com
Register Login

Python Program to check if a string is Pangram or not

|| 0

Python Program to check if a string is Pangram or not
Stechies

What is Panagram Sentence?

A sentence or string is said to be panagram if it contains all the 26 letters of English alphabets at least once. 

For example:

  • “Pack my box with five dozen liquor jugs” is a panagram string
  • "Welcome to my houuse" is not a panagram string

In this tutorial, you will learn how to write a python program to check panagram string using the following methods:

  • Pythonic Naive Method
  • Using ASCII Values of Characters
  • Using Python SET

1) Pythonic Naive Method

#Python program to check STRING is pangram using Pythonic Naive Method

#importing string library
import string

#Taking input from user
inputString = input("Enter the string : ")
#declaring varibale and initilize with the all alphabets
allAlphabets = 'abcdefghijklmnopqrstuvwxyz'
#flag value 0
flag = 0
#iteration for all the characters in the allAlphabets variable
for char in allAlphabets:
    #chaking, Is iterated character is in the string{ In Lowercase }
    if char not in inputString.lower():
        #if yes, Flag value updated
        flag = 1
#chacking flag value and pring the result
if flag == 1:
    print("This is not a pangram string");
else:
    print("It is a pangram string")

OUTPUT

Enter the string : pack my box with five dozen liquor jugs
It is a pangram string

2) Using ASCII Values of Characters

#Python program to check pangram STRING using ASCII Values of Characters 

#importing python library
import itertools
import string

#Taking input from the user
inputString = input("Enter the string : ")
flag = 0;
#checking, If character ascii value is in between the
#ascii value 96 and 123 because lowercase alphabets are
#in between 96 or 123
if sum(1 for char in set(inputString) if 96 < ord(char.lower()) < 123) == 26 :
  #if yes, Flag value updated
  flag = 1
#checking flag value and print the result
if flag == 0:
  print("This is not a pangram string");
else:
  print("It is a pangram string")

Output:

Enter the string : abqwertyuiopasdfgqwertyuiopasdfghjklzxcvbnm
It is a pangram string

3) Using Python Sets 

#Python program to check pangram STRING using Python SET

#Python program to check pangram STRING using Python SET

#importing string library
import string

#Taking input from user
inputString = input("Enter the string : ")
#converting the string into lowercase
lowercaseInputString = inputString.lower()
#remove all space from the string
lowercaseInputString = lowercaseInputString.replace(" ","")
#converting the string into list
inputStringList = set(lowercaseInputString)

#checking the length of list
if len(inputStringList) == 26 :
  print("This is a pangram string")
else:
  print("This is not a pangram string")

Output 1

Enter the string : welcome
This is not a pangram sting

Output 2

Enter the string : the quick brown fox jumps over the lazy dog
It is a pangram string