Count occurrence of character in file using Python

Today in this tutorial, we will get to know how to count the occurrence of a character in a text file using Python. Here we will take a text file ‘text.txt’ as the program’s input and finally display the count of a particular character as output to the user. So, in this tutorial, you will get to know how to take a text file as input, a method to count the occurrence of a particular character, and a Python program to do the same.

Taking a text file as input

We can perform the various tasks on the contents stored in a file. Firstly we have to open the file in a read/write mode. Here, we have to open the text file in the ‘read’ mode. To open the text file named ‘text.txt’ in ‘read’ mode, the Python code is as follows-

file = open("text.txt","r")

Method to count the occurrence of a character in a file

So, here we will see a method to count the occurrence of a particular character in a text file. Firstly, we take the character as an input from the user and open the text file in ‘read’ mode. Now, we have to fetch the contents of the file character by character and find the occurrences of that character. So, to count the occurrence follow these steps-

  • Read one character from the file.
  • Compare this with the character whose count is to be determined.
  • If the comparison result returns true, increment count by 1.
  • Otherwise, fetch the next character from the file.
  • Continue the above operations until you reach the end-of-file (EOF).
  • Display the number of occurrences of the character given by the user.

Python program to count the occurrence of a character in a file

So, let us see a Python program that counts the occurrence of a particular character in a text file. Here, the name of the file used as input is ‘text.txt’. Also, we can take the name of the file from the user. But for simplicity, we take the character from the user only. We check the contents of the file character by character until we reach the end-of-file (EOF). If a character matches, then we have to increment the count by 1. The Python program for counting the occurrence of a character in a file is given below-

count = 0
char = input("ENTER CHARACTER : ")
file = open("text.txt","r")
for i in file:
  for c in i:
    if c == char:
      count = count + 1
print("THE CHARACTER {} IS FOUND {} TIMES IN THE TEXT FILE".format(char,count))

Python program output

The above Python program returns the count of occurrence of a particular character. The contents of file ‘text.txt’ which we use as input is-

This is a sample text file.
Using this file as input we will run a Python program.
The character is entered by the user.

The output of the above program is-

[email protected]:~/python$ python3 char_count.py
ENTER CHARACTER : T
THE CHARACTER T IS FOUND 2 TIMES IN THE TEXT FILE
[email protected]:~/python$

Here, the program displays the count of occurrence of the character ‘T’ in the file. So, the character ‘T’ is found 2 times in the contents of file ‘text.txt’.

Leave a Reply

Your email address will not be published. Required fields are marked *