Find the number of words with 5 Python characters

Asked 2 years ago, Updated 2 years ago, 17 views

fd = open('words_alpha.txt', 'r')
lines = fd.readlines()
fd.close()
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?

python

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

string[start:end:step]

https://www.freecodecamp.org/news/how-to-substring-a-string-in-python/

Search keyword: how to substring in python


2022-09-20 11:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.