STEM

Creating a Pangram Phrase with Python

Introduction

A pangram is a sentence that contains every letter of the alphabet at least once. The most famous example in English is “The quick brown fox jumps over the lazy dog.” Pangrams are not only fun to create but also useful in various applications, such as testing fonts and keyboards. Technically, a pangram doesn’t necessarily have to be a grammatically correct sentence. The goal of a pangram is to include every letter of the alphabet at least once. However, creating a grammatically correct and meaningful pangram can be more memorable, challenging and interesting.

In this blog post, I’ll walk you through a Python script that generates a pangram phrase using a curated list of valid English words. This script ensures efficiency (attempts to generate different pangrams with fewer words, and uses smarter logic to find new, valid words to use) and introduces randomness to create different pangram phrases each time it runs.

How It Works

Fetching the Words: The script starts by fetching the list of words from a provided URL using the requests library.
Initialization: It initializes a set of all lowercase English letters and two empty collections: one for the words used in the pangram and another for the letters covered so far.
Shuffling and Sorting: To introduce randomness, the list of words is shuffled. Then, the words are sorted by the number of unique letters they contain, in descending order. This prioritizes words that contribute the most new letters to the pangram.
Forming the Pangram: The script iterates through the sorted words, adding each word that contributes new letters to the current set of letters. The loop breaks once all letters of the alphabet are covered. The design uses ‘set’ objects to facilitate the operations.
Output: Finally, the words are joined to form the pangram phrase, and the phrase along with the number of words used is printed.

Example Output

When you run the script, it will generate a pangram phrase using the words from the provided URL. Here’s an example output:

You can run the code right here from this page and see what pangram you get! Just click Run on the widget below.

The Code

I hope this was educational and interesting. You can get the full source code from my Patreon shop here (secure transaction).

The list of words is licensed (purchased) from NASPA and cannot be freely shared outside the scope of this program (it must be individually licensed).

Conclusion

Creating a pangram phrase is a fascinating exercise that combines programming logic with linguistic creativity. This Python script not only ensures efficiency but also adds an element of randomness, making each run unique. Feel free to experiment with different word lists and see what interesting pangrams you can generate! Happy coding!

In the next blog, I share code for checking for a pangram where you can enter a phrase and the program will tell you if it’s a pangram or not. Check it out below:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top