In this program, we will count the total number of characters in a file
Steps to solve the program
- Create a variable to count the total number of characters and assign its value equal to 0.
- Open the desired file using open() in reading mode.
- Use for loop iterate over the data.
- If the element is character then add 1 to the variable that we have created.
- Use isalpha() for this purpose.
- Print the output.
char=0
f=open("file2.txt","r")
f1=f.read()
for ele in f1:
if ele.isalpha():
char+=1
f.close()
print("Number of characters: ",char)
Output :
26