Open In App

Python set to check if string is pangram

Last Updated : 25 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string, check if the given string is a pangram or not.

Examples: 

Input : The quick brown fox jumps over the lazy dog
Output : The string is a pangram

Input : geeks for geeks
Output : The string is not pangram

A normal way would have been to use frequency table and check if all elements were present or not. But using import ascii_lowercase as asc_lower we import all the lower characters in set and all characters of string in another set. In the function, two sets are formed- one for all lower case letters and one for the letters in the string. The two sets are subtracted and if it is an empty set, the string is a pangram.

Below is Python implementation of the above approach: 

Python




# import from string all ascii_lowercase and asc_lower
from string import ascii_lowercase as asc_lower
 
# function to check if all elements are present or not
def check(s):
    return set(asc_lower) - set(s.lower()) == set([])
     
# driver code
string ="The quick brown fox jumps over the lazy dog"
if(check(string)== True):
    print("The string is a pangram")
else:
    print("The string isn't a pangram")


Output

The string is a pangram

Time Complexity: O(n)
Auxiliary Space: O(1) 

Method #2: Using lower(),replace(),list(),set(),sort() and join() methods

Python3




# function to check if all elements are present or not
 
string ="The quick brown fox jumps over the lazy dog"
string=string.replace(" ","")
string=string.lower()
x=list(set(string))
x.sort()
x="".join(x)
alphabets="abcdefghijklmnopqrstuvwxyz"
if(x==alphabets):
    print("The string is a pangram")
else:
    print("The string isn't a pangram")


Output

The string is a pangram

Time Complexity : O(N logN)
Auxiliary Space : O(N)

Method #3 : Using lower(),replace(),len() methods and for loop

Approach

  1. Convert the given string to lowercase and remove all spaces
  2. Check whether all the alphabets are present in given string by incrementing count variable and for loop
  3. If count is equal to 26(number of alphabets) display The string is a pangram
  4. If not display The string isn’t a pangram

Python3




# function to check if all elements are present or not
 
string ="The quick brown fox jumps over the lazy dog"
string=string.replace(" ","")
string=string.lower()
alphabets="abcdefghijklmnopqrstuvwxyz"
c=0
for i in alphabets:
    if i in string:
        c+=1
if(c==len(alphabets)):
    print("The string is a pangram")
else:
    print("The string isn't a pangram")


Output

The string is a pangram

Time Complexity : O(N) N – total number of alphabets

Auxiliary Space : O(1) 



Similar Reads

Python program to check if given string is pangram
Given a string, write a Python program to check if that string is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet. Examples: Input : The quick brown fox jumps over the lazy dog Output : Yes Input : abcdefgxyz Output : No We have already discussed the naive approach of pangram checking in this article. Now, le
5 min read
C++ program to check whether a String is a Pangram or not
Given string str, the task is to check whether a string is pangram or not using in C++. A string is a Pangram if the string contains all the English alphabet letters. Examples: Input: str = "We promptly judged antique ivory buckles for the next prize" Output: Yes Explanations: In the above string, str has all the English alphabet letters. Input: st
6 min read
Check if a given string is Pangram in Java
Given string str, the task is to write Java Program check whether the given string is a pangram or not. A string is a pangram string if it contains all the character of the alphabets ignoring the case of the alphabets. Examples: Input: str = "Abcdefghijklmnopqrstuvwxyz"Output: YesExplanation: The given string contains all the letters from a to z (i
3 min read
Check if a String can be converted to Pangram in K changes
Given a String str containing only lowercase English alphabets and an integer K. The task is to check that whether the string can be converted to a Pangram by performing at most K changes. In one change we can remove any existing character and add a new character. Pangram: A pangram is a sentence containing every letter in the English Alphabet. Not
6 min read
Check if given String is Pangram or not
Given a string Str. The task is to check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet. Examples: Input: "The quick brown fox jumps over the lazy dog" Output: is a Pangram Explanation: Contains all the characters from ‘a’ to ‘z’] Input: "The quick brown fox jumps over the dog"Output: is not a Pangr
13 min read
Using Set() in Python Pangram Checking
Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet. Lowercase and Uppercase are considered the same. Examples: Input : str = 'The quick brown fox jumps over the lazy dog' Output : Yes // Contains all the characters from ‘a’ to ‘z’ Input : str='The quick brown fox jumps over the dog'
3 min read
Check if a number is a Pangram or not
Given an integer N, the task is to check whether the given number is a pangram or not. Note: A Pangram Number contains every digit [0- 9] at least once. Examples: Input : N = 10239876540022Output : YesExplanation: N contains all the digits from 0 to 9. Therefore, it is a pangram. Input : N = 234567890Output : NoExplanation: N doesn't contain the di
7 min read
Cost to make a string Pangram
Given an array arr[] containing the cost of adding each alphabet from (a - z) and a string str which may or may not be a Pangram. The task is to find the total cost to make the string Pangram. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}, str = "abcdefghijklmopqrstuvwz " Ou
12 min read
Missing characters to make a string Pangram
Pangram is a sentence containing every letter in the English alphabet. Given a string, find all characters that are missing from the string, i.e., the characters that can make the string a Pangram. We need to print output in alphabetic order. Examples: Input : welcome to geeksforgeeksOutput : abdhijnpquvxyzInput : The quick brown fox jumpsOutput :
8 min read
Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
Given a tree, and the weights (in the form of strings) of all the nodes, the task is to count the nodes whose weighted string when concatenated with the strings of the sub-tree nodes becomes a pangram. Pangram: A pangram is a sentence containing every letter of the English Alphabet. Examples: Input: Output: 1 Only the weighted string of sub-tree of
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg