Find the number of words with 5 Python characters

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

fd = open('words_alpha.txt', 'r')
lines = fd.readlines()
print('number of all words in the dictionary =', len(lines)))
print('number of five-letter words in the dictionary = ', len(lines))))
for line in lines:
    line = line.strip()

    if len(line) == 5:
       print(line ,len(line))

How do we get the five-letter word count here?


2022-09-20 11:01

2 Answers

words5 = [ line.strip() for line in lines if len(line.strip()) == 5 ]

If you do this, words5 will only collect words of length 5. If you don't know this grammar, you should search for list compliance and list abbreviation.

2022-09-20 11:01


Search keyword: how to substring in python

2022-09-20 11:01

If you have any answers or tips

© 2022 pinfo. All rights reserved.