Write a function in Python to print those words which contains letter ‘S’ or ‘s’
anywhere in the word in text file “STORY.txt”.
If the “STORY.txt” contents are as follows:
An Old Man is happy today, he doesn’t
complain about anything, smiles, and even
his face is freshened up.
The output of the function should be:
is doesn’t smiles his is freshened
Answers
Answered by
7
def main():
words = []
with open("STORY.txt", "r") as f:
for line in f:
for word in line.split():
if "s" in word or "S" in word:
words = [*words, word]
for idx, word in enumerate(words):
if "," in word:
words[idx] = word.replace(",", "")
return " ".join(words)
print(main())
Similar questions