WAP to read the content of the file and count how many times letter 'a' comes in file
class 12 python chaptet data handling
Answers
The given problem is solved using language - Python.
file=open('TextFile.txt','r')
counter=0
content=file.read()
for i in content:
if i in 'Aa':
counter=counter+1
print('File Content:\n\n',content)
print('\nNumber of times the letter \'a\' present:',counter)
- Open the file in reading mode.
- Initialize counter = 0.
- Read the content of the file.
- Go through the file content.
- If letter 'a' appears, increment the value of counter.
- Display the counter.
The output for the given program is:
File Content:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Number of times the letter 'a' present: 29