How to count the number of lines, words, and characters in Python
In this Answer, we'll learn to count the number of lines, words, and characters present in a file.
The basic idea is to traverse each line in a file and count the number of words and characters.
Example 1
Let's take a look at the following example:
number_of_words = 0number_of_lines = 0number_of_characters = 0with open("data.txt", 'r') as file:for l in file:number_of_words += len(l.split())number_of_lines += 1number_of_characters = len(l)print("No of words: ", number_of_words)print("No of lines: ", number_of_lines)print("No of characters: ", number_of_characters)
Code explanation
In the above code snippet:
Lines 1–4: We declare and initialize variables with value
0to store the total count of words, characters, and lines.Line 5: We open the file in reading mode
r.Line 6: We loop through each line in the file using the
forloop.Line 7: We get the list of words present in a line using the
split()method. We calculate the length/number of words passing the result of thesplit()method to thelen()function and add it to thenumber_of_wordsvariable.Line 8: We add value
1tonumber_of_linesvariable as we progress through each line in a file.Line 9: We get the count of characters using the
len()function and add the result to thenumber_of_charactersvariable.
Once we traverse each line in a file, we get the count of lines, words, and characters present in a file.
Example 2
def count_lines_words_chars(filename):number_of_words = 0number_of_lines = 0number_of_characters = 0with open("data.txt", 'r') as file:for l in file:number_of_words += len(l.split())number_of_lines += 1number_of_characters = len(l)return number_of_lines, number_of_words, number_of_characterswith open("data.txt", 'r') as file:content = file.read()number_of_lines = content.count('\n') + 1 # Counting the number of newline charactersnumber_of_words = len(content.split())number_of_characters = len(content)print("Lines:", number_of_lines)print("Words:", number_of_words)print("Characters:", number_of_characters)
Explanation
Line 16: This counts the number of newline characters (
\n) in the content and adds1to account for the last line which might not end with a newline character, thus giving the total count of lines.
Free Resources