Write a program to count the number of uppercase alphabets present in a text file “Poem.txt”

Here’s a Python program that counts the number of uppercase alphabets in a text file called “Poem.txt”:

def count_uppercase(filename):
    try:
        with open(filename, 'r') as file:
            content = file.read()
            uppercase_count = 0

            for char in content:
                if char.isupper():
                    uppercase_count += 1

            return uppercase_count

    except FileNotFoundError:
        print(f"File '{filename}' not found.")
        return None


filename = 'Poem.txt'
result = count_uppercase(filename)

if result is not None:
    print(f"Number of uppercase alphabets: {result}")

 

Make sure you have a text file named “Poem.txt” in the same directory as the Python script. The program reads the file character by character and checks if each character is an uppercase alphabet using the isupper() method. If a character is uppercase, it increments the uppercase_count variable. Finally, it prints the total count of uppercase alphabets.


Comments

Leave a Reply

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