Week 5 assignment: len() vs __len__()

count = 0

with open('poem.txt', 'r') as file:

    for line in file.readlines():

        count += line.__len__()


I do not understand the line.len() function for counting the words in a file. Please explain, thanks!

Hi @theerthasenan,
Here line is a list, which contains all the strings.
__len__() is just a method which returns the length or the string here.

what is the difference between len() and __len__()

I believe that the solution here is incorrect as __len__() provides number of characters whereas, the question is asking us to count number of words.

Yes, it should have been
line.split(" ").__len__()

1 Like