In this program, we will find the total number of special characters in a file.
Steps to solve the program
- Create a variable to count the total number of special 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 character is a special character then add 1 to the variable that we have created.
- Use isalnum() for this purpose.
- Print the output.
f1=open("file1.txt","r")
data=f1.read()
is_special = 0
for char in data:
if char.isalnum():
is_special += 1
print("NUmber of digits: ",is_special)
Output :
3