with open("simple.in", encoding="utf-8") as f: lines = f.readlines() for line in lines: line = line.strip() line = line.split() for i in range(len(line)-1): if line[i][:2] == "19": if 0 < int(line[i][3:4]) < 99: if line[i+1][:2] == "r.": print(" ".join(line))