Python 3 program to check if a string is pangram or not

Python 3 program to check if a string is pangram or not :

In this tutorial, we will learn how to check if a string is pangram or not using python 3. A pangram string contains every letter of the alphabet. For example, ‘the quick brown fox jumps over the lazy dog’ is a pangram. But ‘abcdefghij’ is not.

Program :

def check_pangram(arg):
	if len(set('abcdefghijklmnopqrstuvwxyz') - set(arg.lower())) == 0 :
		return True

	return False

user_str = input("Enter a string to check for pangram : ")

if(check_pangram(user_str)):
	print("It is a pangram string")
else:
	print("Not a pangram string")

Output :

Enter a string to check for pangram : The quick brown fox jumps over the lazy dog
It is a pangram string

Enter a string to check for pangram : abcdefghijklmnopqrstuvwxyz
It is a pangram string

Enter a string to check for pangram : abcdefgh
Not a pangram string

Explaination :

‘check_pangram’ method takes one string as its argument and checks if it is pangram or not. The main idea of this method is that ‘set’ cannot contain duplicate elements. So, if we create a set of all characters of the input string, it will filter out the characters. Before creating a set, we convert all characters to lowercase using ‘lower’.

For example, for string ‘Hello World‘, the set will contain ‘e,l,h,o,w,r,d’ . We already have one set with all characters set(‘abcdefghijklmnopqrstuvwxyz’). So, if we subtract the new set from this set, it will contain the elements that exist in set(‘abcdefghijklmnopqrstuvwxyz’) and don’t exist in the new set.
We are checking the count of the final set. If the count is ‘0’, means the second set contains all alphabets. In this case, return ‘True’. If count is not ‘0’, return ‘False’.

Leave a Reply