For ordinary text, write a string containing n newline characters inside a with open(...) block. Use "w" to replace a file or "a" to add to it, and specify UTF-8 explicitly.
Write multiple lines from one string
If your content is already a multiline string, pass it to write(). Python writes the string as given; the newline characters separate its lines.
with open("output.txt", "w", encoding="utf-8") as file:
file.write("First linenSecond linenThird linen")
The last n is optional. Include it if you want the file to end with a line terminator. In text mode, Python translates n to the platform’s default line ending. The Python tutorial notes that write() returns the number of characters written.
Write a list of lines
When each line is already a separate string, you can use writelines()—but it does not add newline characters or any other separators. Include the separator in each string:
#1 Best Overall
lines = ["First linen", "Second linen", "Third linen"]
with open("output.txt", "w", encoding="utf-8") as file:
file.writelines(lines)
The Python I/O reference says line separators are not added, so each supplied string usually needs one. If your strings do not already end in newlines, join them instead:
lines_without_newlines = ["First line", "Second line", "Third line"]
with open("output.txt", "w", encoding="utf-8") as file:
file.write("n".join(lines_without_newlines) + "n")
Write values one line at a time
For values that need conversion or formatting, loop over them and write each formatted string with a newline. Text-mode write() expects a string, so convert numbers or other values before writing.
Rank #2
values = ["alpha", "beta", "gamma"]
with open("output.txt", "w", encoding="utf-8") as file:
for value in values:
file.write(f"{value}n")
Choose whether to replace or append
The mode passed to open() determines what happens to existing content:
"w"creates the file if needed and erases its existing contents when opened."a"creates the file if needed and adds new writes at the end.
For example, change the mode in the open() call to append:
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitcheswith open("output.txt", "a", encoding="utf-8") as file:
file.write("An additional linen")
Use a context manager and an explicit encoding
Opening the file with with ensures it is closed when the block exits, including if an exception occurs. Closing also allows buffered output to be handled reliably. The Python tutorial recommends specifying an encoding such as "utf-8" for ordinary text because the default encoding can depend on the platform.
Text files and binary files are different
These examples are for text. Text mode handles newline translation; if you are writing raw bytes such as JPEG or EXE data, use binary mode instead. Text encoding and newline handling are not a substitute for writing binary data.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




