with open("file.txt") as f: out = [] for line in f: for word in line.split(): out.append(word) if word.endswith(('.', '!')): print(' '.join(out)+' ') out.clear()