Text Files · 14 / 17 Exercise Medium

Search lines in a text file

Objective

Create a Python program that reads the contents of a text file and stores its lines in a list of strings. Then ask the user for words or phrases until an empty text is entered. Each time the user enters a word or phrase, the program shows the lines that contain it.

Files: the file text.txt already exists. Read its lines into a list; then read words or phrases with input() until an empty line is entered. For each one, print the lines of the file that contain it (the search is case-sensitive), in the order they appear in the file.

Input
dolor
tempor
Output
Lorem ipsum dolor sit amet
sed do eiusmod tempor

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise