使用此習慣用法讀取文件時: with open("line.txt") as f: for line in f: line結尾帶有一個\n字符。 Try this: with open("line.txt") as f: for line in f: line = line.strip() # Removes the "\n" character for word in line.split(): if word == 'Way': line = line.replace("Way", "Street") print(line, end="\n") # Puts back the "\n" character. 或者你可以使用print(line, end="")。默認情況下,print()以\n字符結尾,您可以將end=""指定為,以避免額外的換行符,該行在讀取時不分條,即。 with open("line.txt") as f: for line in f: for word in line.split(): if word == 'Way': line = line.replace("Way", "Street") print(line, end="")