with open("shakespeare.in", encoding="utf8") as f: lines = f.readlines() for line in lines: line = line.strip() line = line.split() for word in line: if word[:6] == "Hamlet": result = " ".join(line) print(result) break