Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Check if a given string is binary string or not - Python
Next article icon

The task is to check if astring is apangramwhich means it includes every letter of the English alphabet at least once. In this article, we’ll look at different ways to check if the string contains all 26 letters.

Using Bitmasking

Bitmasking uses a number where each bit represents a letter in the alphabet. It updates the bits as it finds letters in the string and checks if all 26 letters are present.

Example:

Python
# Input strings="The quick brown fox jumps over the lazy dog"# Initialize bitmask for tracking found lettersf=0# Loop through each character in lowercase stringforcharins.lower():ifchar.isalpha():# Check if the character is alphabeticf|=1<<(ord(char)-ord('a'))# Set corresponding bit for letteriff==(1<<26)-1:# All letters foundprint(True)# It's a pangrambreakelse:print(False)# Not a pangram

Output
True

Using sets

We can usesets to store unique characters and perform fast checks.string.ascii_lowercase gives astring of all lowercase letters, which can be turned into a set for easy comparison.

Example:

Python
importstring# Input stringa="The quick brown fox jumps over the lazy dog"# Create set of all lowercase English lettersb=set(string.ascii_lowercase)# Convert input text to lowercase# Create a set of characterss=set(a.lower())# Check if `s` contains all letters of `b`res=b<=sprint(res)

Output
True

Using all()

all() function checks if every condition in a list is true. Here, it checks if every letter of the alphabet is in the given text and returns True only if all letters are found.

Example:

Python
importstring# Input texts="Python is great"# Check if all letters are in `s`res=all(letterins.lower()forletterinstring.ascii_lowercase)print(res)

Output
False

Using count()

count() method checks how many times each letter appears in the string. It repeatedly does this for all letters of the alphabet to see if the string contains every letter.

Example:

Python
importstring# Input texts="The quick brown fox jumps over the lazy dog."# Check if each letter of the alphabet appears in `t`forletterinstring.ascii_lowercase:ifs.lower().count(letter)==0:print(False)breakelse:print(True)

Output
True

Explanation:

  • string.ascii_lowercase provides all lowercase letters to check against the input string.
  • s.lower().count(letter) This checks if each letter appears in `s`, prints False if any letter is missing.

Similar Reads

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 ourCookie 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

[8]ページ先頭

©2009-2025 Movatter.jp