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:

main.py
data.txt
number_of_words = 0
number_of_lines = 0
number_of_characters = 0
with open("data.txt", 'r') as file:
for l in file:
number_of_words += len(l.split())
number_of_lines += 1
number_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 0 to 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 for loop.

  • 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 the split() method to the len() function and add it to the number_of_words variable.

  • Line 8: We add value 1 to number_of_lines variable 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 the number_of_characters variable.

Once we traverse each line in a file, we get the count of lines, words, and characters present in a file.

Example 2

main.py
data.txt
def count_lines_words_chars(filename):
number_of_words = 0
number_of_lines = 0
number_of_characters = 0
with open("data.txt", 'r') as file:
for l in file:
number_of_words += len(l.split())
number_of_lines += 1
number_of_characters = len(l)
return number_of_lines, number_of_words, number_of_characters
with open("data.txt", 'r') as file:
content = file.read()
number_of_lines = content.count('\n') + 1 # Counting the number of newline characters
number_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 adds 1 to account for the last line which might not end with a newline character, thus giving the total count of lines.

Copyright ©2024 Educative, Inc. All rights reserved