Sentence validation: is a pangram in Python?

Python Methods and Functions

A proposal is given. Our task &check if this sentence is pan-grams or not. The logic behind grammar checking is that words or sentences contain each letter of the alphabet at least once. To solve this problem, we use the Python set () method and list comprehension technique.





Example

 Input: string = 'abc def ghi jkl mno pqr stu vwx yz' Output: Yes // contains all the characters from 'a' to 'z' Input: str = 'python program' Output: No // Does not contains all the characters from 'a' to' z' 



Algorithm

 Step 1: create a string. Step 2: Convert the complete sentence to a lower case using lower () method. Step 3: convert the input string into a set (), so that we will list of all unique characters present in the sentence. Step 4: separate out all alphabets ord () returns ASCII value of the character. Step 5: If length of list is 26 that means all characters are present and sentence is Pangram otherwise not. 



Sample code

 def checkPangram (s): lst = [] for i in range (26): lst. append (False) for c in s.lower (): if not c == "": lst [ord (c) -ord ('a')] = True for ch in lst: if ch == False: return False ret urn True # Driver Program str1 = input ("Enter The String :: 7gt;") if (checkPangram (str1)): print ('" '+ str1 +' "') print ("is a pangram") else: print ('" '+ str1 +' "') print ("is not a pangram") 



Output

 Enter The String :: abc def ghi jkl mno pqr stu vwx yz "abc def ghi jkl mno pqr stu vwx yz" is a pangram Enter The String :: & gt; python program "pyhton program" is not a pangram 







Get Solution for free from DataCamp guru