write a program in python to count the bo of yppercase alphabet present in txt file in python
Answers
To achieve this, we will first need to open the text file, read the file and then check for the upper case letters.
To open a text file, we use open() but keep in mind that the text file should be in the same directory as your python file.
Coming back, open() syntax is:
open(textfile , function)
For function, there are 2 types. Read and write. What we want is to read the text file, so we use 'r+'
opening = open('filename.txt' , 'r+')
Now, we need to read the text inside the file.
text = opening.read()
Now, if we print text, the output would be all the text inside the file. Now comes the second step, checking for the upper case letters.
To see if any alphabet is a upper case, we use .isupper()
To do this, we will have to use the for loop to iterate over all the characters in text.
CODE:
opening = open('filename.txt' , 'r+')
text = opening.read()
opening.close() #Always close the file
count = 0
for letter in text:
if letter.isupper():
count += 1
print(f'Total number of upper case letters are {count}')