Word Search in File
Write a program that saves several lines of text to a file, then reads it back line by line and prints only the lines containing a given search word.
What the problem means: search through saved text file content for a specific word, one line at a time — the most common, memory-efficient way to scan a file.
Approach: write the given lines to a file, then loop for line in file: and print any line where the search word is found (using the in operator).
Input: First line: the number of lines n. Next n lines: the text content. Final line: the search word.
Output: Every line (in order) that contains the search word, printed as-is (without the trailing newline).
3 I love Python Java is great Python is fun Python
I love Python Python is fun
- 1 <= n <= 1000
Hint 1
Write each line to the file first (with a trailing \n), just like the Line Counter problem.
Hint 2
for line in file: reads one line at a time, which already includes its trailing newline.
Hint 3
Check membership with search_word in line, and print(line.strip()) to drop the extra newline from the read line.
Save every input line to a file first, then reopen it and loop for line in file: — this reads it one line at a time. For each line, check if search_word in line; if so, print(line.strip()) to show it without the extra newline that reading a line always includes.