Count Characters in File

Count the total number of characters in a text file.

PythonBeginner
Python
# Program to count characters in a file

filename = input("Enter filename: ")

try:
    with open(filename, "r", encoding="utf-8") as f:
        text = f.read()
    print("Character count:", len(text))
except FileNotFoundError:
    print("File not found.")

Output

Enter filename: notes.txt
Character count: 120

len(text) returns the total number of characters including spaces and newlines.