Python Program to Display Words Starting with a Vowel and Ending with a Digit from a Text File

Introduction

Text processing is a core aspect of programming, and the ability to extract specific patterns from text files is a valuable skill. In this article, we’ll explore how to write a Python program to extract words that start with a vowel and end with a digit from a text file.

Getting Started

Let’s break down the process of achieving this task:

  1. Open the File: We’ll begin by opening the text file using the `open()` function.
  2. Read and Process Words: With the file open, we’ll read its contents word by word, splitting at spaces.
  3. Pattern Matching: For each word, we’ll check if it starts with a vowel (A, E, I, O, U, or their lowercase counterparts) and ends with a digit (0-9).
  4. Display Matching Words: If the word satisfies both conditions, we’ll display it.

Writing the Code

Let’s break down the process of achieving this task:


def extract_words(file_name):
    try:
        with open(file_name, 'r') as file:
            vowels = 'aeiouAEIOU'
            matching_words = []
            
            for line in file:
                if line:
                    words = line.split()
                    for word in words:
                        if word[0] in vowels and word[-1].isdigit():
                            matching_words.append(word)
            
            return matching_words
    except FileNotFoundError:
        print("File not found.")
        return []

if __name__ == "__main__":
    file_name = input("Enter the name of the text file: ")
    matching_words = extract_words(file_name)
    
    if matching_words:
        print("Words starting with a vowel and ending with a digit:")
        for word in matching_words:
            print(word)
    else:
        print("No matching words found.")

Understanding the Code

  1. The `extract_words()` function takes a filename as an argument.
  2. Within the function, we define the string `vowels` containing all vowel characters.
  3. We initialize an empty list called `matching_words` to store the words that satisfy the conditions.
  4. The program reads the file line by line, splitting each line into individual words.
  5. For each word, we check if its first character is a vowel and its last character is a digit. If both conditions are met, the word is added to the `matching_words` list.
  6. In the `__main__` block, the user is prompted to provide the name of the text file.
  7. The `extract_words()` function is called with the provided file name, and the matching words are stored in the `matching_words` list.
  8. If there are matching words, the program displays them. Otherwise, it notifies the user that no matching words were found.

Running the Program

  1. Save the code in a `.py` file (e.g., `vowel_digit_extractor.py`).
  2. Place the text file you want to analyze in the same directory as the Python script.
  3. Open a terminal and navigate to the directory containing the script.
  4. Run the script using the command: `python vowel_digit_extractor.py`.
  5. Follow the prompts to enter the name of the text file.
  6. The program will display the words that start with a vowel and end with a digit from the specified text file.

Conclusion

In this article, we’ve explored how to develop a Python program that extracts words starting with a vowel and ending with a digit from a text file. This process involves text file handling, string manipulation, and pattern matching, all of which are fundamental skills for any programmer working with textual data. This program serves as a practical example of how Python’s versatility can be harnessed to perform complex tasks efficiently.

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 147

Leave a Reply

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