Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Using Set() in Python Pangram Checking
Next article icon

Python set to check if string is pangram

Last Updated : 25 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
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) 



Next Article
Using Set() in Python Pangram Checking

S

Striver
Improve
Article Tags :
  • DSA
  • Python
  • Strings
  • python-set
Practice Tags :
  • python
  • python-set
  • Strings

Similar Reads

  • 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’ In
    3 min read
  • Check If String is Integer in Python
    In this article, we will explore different possible ways through which we can check if a string is an integer or not. We will explore different methods and see how each method works with a clear understanding. Example: Input2 : "geeksforgeeks"Output2 : geeksforgeeks is not an IntigerExplanation : "g
    4 min read
  • Python - Check if substring present in string
    The task is to check if a specific substring is present within a larger string. Python offers several methods to perform this check, from simple string methods to more advanced techniques. In this article, we'll explore these different methods to efficiently perform this check. Using in operatorThis
    2 min read
  • Check if a string is Pangrammatic Lipogram
    To understand what a pangrammatic lipogram is we will break this term down into 2 terms i.e. a pangram and a lipogram Pangram: A pangram or holoalphabetic sentence is a sentence using every letter of a given alphabet at least once. The best-known English pangram is "The quick brown fox jumps over th
    14 min read
  • Check if String Contains Substring in Python
    This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx
    8 min read
  • Python - Check for float string
    Checking for float string refers to determining whether a given string can represent a floating-point number. A float string is a string that, when parsed, represents a valid float value, such as "3.14", "-2.0", or "0.001". For example: "3.14" is a float string."abc" is not a float string.Using try-
    2 min read
  • Check if given String is Pangram or not
    Given a string s, the task is to check if it is Pangram or not. A pangram is a sentence containing all letters of the English Alphabet. Examples: Input: s = "The quick brown fox jumps over the lazy dog" Output: trueExplanation: The input string contains all characters from ‘a’ to ‘z’. Input: s = "Th
    6 min read
  • Python Set | Check whether a given string is Heterogram or not
    Given a string S of lowercase characters. The task is to check whether a given string is a Heterogram or not using Python. A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. Input1: S = "the big dwarf only jumps"Output1: YesExplanation: Each alphabe
    3 min read
  • Cost to make a string Panagram | Set 2
    Given an array cost[] containing the cost of adding each alphabet from (a – z) and a string str consisting of lowercase English alphabets which may or may not be a Panagram. The task is to make the given string a Panagram with the following operations: Adding a character in str costs twice the cost
    8 min read
  • Collections.UserString in Python
    Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one. Example: C/C++ Code # Python program to demonstrate # string # Creating a String # with single Quotes String1 = 'Welcome to the Geeks Worl
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences