Write a Python Function to Check Whether a String is Pangram or Not

Write a Python Function to Check Whether a String is Pangram or Not

A pangram is a sentence or phrase that contains all 26 letters of the English alphabet at least once. Pangrams are often used to test the capabilities of typewriters, keyboards, and fonts. They can also be used to improve typing skills and to learn new words.

Python is a popular programming language that is known for its simplicity and readability. It is also a very versatile language that can be used for a variety of tasks, including writing pangram checkers.

In this blog post, we will show you how to write a Python function to check whether a string is a pangram or not.

Step 1: Import the necessary libraries

Python
import string

Step 2: Define the function

Python
def is_pangram(string):
  """Checks whether a string is a pangram or not.

  Args:
    string: A string to check.

  Returns:
    True if the string is a pangram, False otherwise.
  """

  alphabet = set(string.lower())
  for letter in string.lower():
    if letter not in alphabet:
      return False
  return True

Step 3: Use the function

Python
string = "The quick brown fox jumps over the lazy dog."

if is_pangram(string):
  print("The string is a pangram.")
else:
  print("The string is not a pangram.")

Output:

The string is a pangram.

Other approaches

There are other ways to write a Python function to check whether a string is a pangram or not. One approach is to use a regular expression to remove all non-alphabetic characters from the string. Then, you can use a dictionary to count the number of times each letter appears in the string. If all 26 letters appear at least once, then the string is a pangram.

Another approach is to use a set to store all of the letters in the alphabet. Then, you can iterate over the string and remove each letter from the set. If the set is empty at the end of the iteration, then the string is a pangram.

Conclusion

Writing a Python function to check whether a string is a pangram or not is a simple and straightforward task. The function we have shown you in this blog post is just one of many possible approaches. You can choose the approach that best suits your needs and preferences.

Post a Comment

0 Comments