Python Example – Write a Python program to count the number of each character of a text file

Hits: 2

(Python Example for Citizen Data Scientist & Business Analyst)


Write a Python program to count the number of each character of a text file.

German Unity Day
From Wikipedia, the free encyclopedia
The Day of German Unity (German: Tag der DeutschenEinheit) is the national day of Germany, celebrated on 3 October as a public holiday. It commemorates the anniversary of German reunification in 1990, when the goal of a united Germany that originated in the middle of the 19th century, was fulfilled again. Therefore, the name addresses neither the re-union nor the union, but the unity of Germany. The Day of German Unity on 3 October has been the German national holiday since 1990, when the reunification was formally completed.

Sample Solution:

Python Code :

import collections
import pprint
file_input = input('File Name: ')
with open(file_input, 'r') as info:
  count = collections.Counter(
  value = pprint.pformat(count)

Sample Output:

File Name:  abc.txt
Counter({' ': 93,
         'E': 64,
         'N': 45,
         'A': 42,
         'T': 40,
         'I': 36,
         'O': 31,
         'R': 29,
         'H': 25,
         'D': 19,
         'M': 17,
         'Y': 17,
         'L': 15,
         'F': 15,
         'U': 14,
         'C': 13,
         'G': 13,
         'S': 12,
         ',': 7,
         'B': 6,
         'W': 5,
         '9': 5,
         '.': 4,
         'P': 4,
         '1': 3,
         '\n': 2,
         '0': 2,
         '3': 2,
         ':': 1,
         '-': 1,
         'K': 1,
         '(': 1,
         ')': 1,
         'V': 1})


Write a Python program to count the number of each character of a text file


Sign up to get end-to-end “Learn By Coding” example.



Applied Machine Learning & Data Science with Python, R and SQL.

A list of Python, R and MATLAB Codes for Applied Machine Learning and Data Science


Subscribe to our YouTube channel and press the ring to get latest update on Python and R coding recipesCoding is our strength.


Learn by Coding Categories:


Data Analytics:

Data Science:

Data Visualisation:

Machine Learning Recipe:




Supervised Learning:

Tabular Data Analytics:

End-to-End Data Science Recipes:

Applied Statistics:

Bagging Ensemble:

Boosting Ensemble:



Data Analytics:

Data Science:

Data Visualisation:

Decision Tree:


Machine Learning Recipe:

Multi-Class Classification:

Neural Networks:

Python Machine Learning:

Python Machine Learning Crash Course:

R Classification:

R for Beginners:

R for Business Analytics:

R for Data Science:

R for Data Visualisation:

R for Excel Users:

R Machine Learning:

R Machine Learning Crash Course:

R Regression:



Excel examples for beginners:

C Programming tutorials & examples:

Javascript tutorials & examples:

Python tutorials & examples:

R tutorials & examples:

SQL tutorials & examples:

SQL for Beginners in 2 Weeks:


Portfolio Projects for Aspiring Data Scientists: Tabular Text & Image Data Analytics as well as Time Series Forecasting in Python & R @



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.