Type Here to Get Search Results !

Python Program to Count Capital Letters in a File

Python Program to Count Capital Letters in a File

Writing a program to count the number of capital letters in a file is an important coding question that you can get in any coding interview. You can get questions based on this logic in several ways. You will be given a text file, and you will be asked to read the file without using a Python library and print the number of capital or lowercase letters in the text file. So here’s how you can write a Python program to count capital letters in a text file:

with open("text.txt") as file:
    count = 0
    text = file.read()
    for i in text:
        if i.isupper():
            count += 1
    print(count)

Output:21979

In the code above:

  1. I first opened a text file that was already saved on my computer.
  2. Then I introduced a variable as count, which is used here to store the number of uppercase letters.
  3. Initially, I declared its value to be 0, and in the next line, I am reading the text file
  4. Then I am using a for loop over the content of the text file and using the if statement inside the for loop which will keep adding 1 to the count variable until it keeps finding uppercase letters in the file using the isupper() function in Python.

Just like the above method, you can write a python program for counting small letters also by replacing the isupper() function with islower() as shown in the code below:


with open("text.txt") as file:
    count = 0
    text = file.read()
    for i in text:
        if i.islower():
            count += 1
    print(count)

Output: 652265

Summary

So this is how you can find all capital letters in a file by using the Python programming language. It is an important coding question that you can get in any coding interview. You can get questions based on this logic in several ways. I hope you liked this article on how to write a Python program to count the number of capital letters in a file. Feel free to ask your valuable questions in the comments section below.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.