w3resource

Python Exercise: Check whether a string is a pangram or not

Python Functions: Exercise-14 with Solution

Write a Python function to check whether a string is a pangram or not.

Note : Pangrams are words or sentences containing every letter of the alphabet at least once.
For example : "The quick brown fox jumps over the lazy dog"

Sample Solution:

Python Code:

# Import the 'string' and 'sys' modules
import string
import sys

# Define a function named 'ispangram' that checks if a string is a pangram
def ispangram(str1, alphabet=string.ascii_lowercase):
    # Create a set 'alphaset' containing all lowercase letters from the provided alphabet
    alphaset = set(alphabet)
    
    # Convert the input string to lowercase and create a set from it
    str_set = set(str1.lower())
    
    # Check if the set of lowercase characters in the input string covers all characters in 'alphaset'
    return alphaset <= str_set

# Print the result of checking if the string is a pangram by calling the 'ispangram' function
print(ispangram('The quick brown fox jumps over the lazy dog')) 

Sample Output:

True

Explanation:

In the exercise above the code defines a function named "ispangram()" that checks whether a given string is a pangram (contains all the letters of the alphabet at least once). It utilizes the 'string' module to access the lowercase alphabet and creates a set of all lowercase letters. The function then converts the input string to lowercase, creates a set from it, and checks if the set of lowercase characters in the input string covers all characters in the lowercase alphabet. Finally, it prints the result of checking if the provided string is a pangram or not.

Pictorial presentation:

Python exercises: Check whether a string is a pangram or not.

Flowchart:

Flowchart: Python exercises: Check whether a string is a pangram or not.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python function that that prints out the first n rows of Pascal's triangle.
Next: Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/python-functions-exercise-14.php