Strings in Python:
A Python string is an ordered collection of characters used to express and store text-based data. Strings are saved in an adjacent memory area as individual characters. It is accessible in both directions: forward and backward. Characters are merely symbols. Strings are immutable Data Types in Python, which means they cannot be modified once they are formed.
Pangram:
If a sentence or string contains all 26 letters of the English alphabet at least once, it is considered to be a pangram.
Examples:
Example1:
Input:
given string ="Helloabcdfegjilknmporqstvuxwzy"
Output:
The given string helloabcdfegjilknmporqstvuxwzy is a pangram
Example2:
Input:
given string ="hellothisisbtechgeeks"
Output:
The given string hellothisisbtechgeeks is not a pangram
Python Program to Check if a String is a Pangram or Not
There are several ways to check if the given string is pangram or not some of them are:
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
Method #1:Naive Approach
Approach:
- Scan the given string or provide static input.
- Use the lower() method to convert this string to lowercase.
- The brute force method is to take a string that contains all of the letters of the English alphabet.
- Traverse through all of the alphabet’s characters string
- Check to see if this character appears in the given string.
- If it isn’t present, return False.
- Return True at the end of the loop (which implies it is pangram)
Below is the implementation:
# Python Program to Check if a String is a Pangram or Not
def checkPangramString(string):
# creating a new string (alphabet string which stores all the alphabets of the english language
AlphabetString = 'abcdefghijklmnopqrstuvwxyz'
# Traverse through the alphabets string
for char in AlphabetString:
# Check if this character is present in given string .
if char not in string.lower():
# if yes then this character is not available hence return False
return False
# After the end of loop return True (which implies it is pangram)
return True
# given string
string = "Helloabcdfegjilknmporqstvuxwzy"
# converting the given string into lower case
string = string.lower()
# passing this string to checkPangramString function which returns true
# if the given string is pangram else it will return false
if checkPangramString(string):
print("The given string", string, "is a pangram")
else:
print("The given string", string, "is not a pangram")
Output:
The given string helloabcdfegjilknmporqstvuxwzy is a pangram
Method #2:Using set() method
The following program can be easily implemented in Python by using the set() method.
Approach:
- Scan the given string or provide static input.
- Use the lower() method to convert this string to lowercase.
- Use the set() function to convert this string to a set.
- Determine the length of the set.
- If the length is 26, it is a pangram (since the English language has only 26 alphabets).
- Otherwise, it is not a pangram.
Below is the implementation:
# Python Program to Check if a String is a Pangram or Not
def checkPangramString(string):
# converting given string to set using set() function
setString = set(string)
# calculate the length of the set
length = len(setString)
# If the length is 26, it is a pangram so return true
if(length == 26):
return True
else:
return False
# given string
string = "Helloabcdfegjilknmporqstvuxwzy"
# converting the given string into lower case
string = string.lower()
# passing this string to checkPangramString function which returns true
# if the given string is pangram else it will return false
if checkPangramString(string):
print("The given string", string, "is a pangram")
else:
print("The given string", string, "is not a pangram")
Output:
The given string helloabcdfegjilknmporqstvuxwzy is a pangram
Note:
This method is only applicable if the given string contains alphabets.
Method #3:Using Counter() function (Hashing)
The following program can be easily implemented in Python by using the counter() function
Approach:
- Scan the given string or provide static input.
- Use the lower() method to convert this string to lowercase.
- Calculate the frequency of all characters in the given string using Counter() method
- Calculate the length of the Counter dictionary.
- If the length is 26, it is a pangram (since the English language has only 26 alphabets).
- Otherwise, it is not a pangram.
Below is the implementation:
# Python Program to Check if a String is a Pangram or Not
# importing counter from collections
from collections import Counter
def checkPangramString(string):
# Calculate the frequency of all characters in the given string
# using Counter() method
frequ = Counter(string)
# calculate the length of the frequency dictionary which is
# returned from counter() function
length = len(frequ)
# If the length is 26, it is a pangram so return true
if(length == 26):
return True
else:
return False
# given string
string = "Helloabcdfegjilknmporqstvuxwzy"
# converting the given string into lower case
string = string.lower()
# passing this string to checkPangramString function which returns true
# if the given string is pangram else it will return false
if checkPangramString(string):
print("The given string", string, "is a pangram")
else:
print("The given string", string, "is not a pangram")
Output:
The given string helloabcdfegjilknmporqstvuxwzy is a pangram
Note:
This method is only applicable if the given string contains alphabets.
Related Programs:
