Recommended Free Tools
Use file.write(text) and leave "n" out of the string. Python’s write() method writes the supplied string as-is; it does not add a newline automatically. If you prefer print(), send it to the file with file=f and set end="".
Write a string without adding a newline
For exact text output, call write() on the open file:
with open("output.txt", "w", encoding="utf-8") as f:
f.write("Hello")
This writes Hello without a line ending. A newline is part of the string’s contents, so include "n" only when you want a line break:
f.write("Hellon")
The Python tutorial describes f.write(string) as writing the string’s contents and returning the number of characters written: Python tutorial: reading and writing files.
#1 Best Overall
Use print() without its trailing newline
print() adds a newline by default. To write through it without a trailing line ending, pass the file handle and set end="":
with open("output.txt", "w", encoding="utf-8") as f:
print("Hello", file=f, end="")
The official tutorial uses end='' when printing lines so that print() does not add another newline: Python tutorial: reading and writing files. The built-in documentation also explains the file and end arguments: Python built-in function print().
Rank #2
Choose between write() and print()
| Method | Best for | Newline behavior |
|---|---|---|
f.write(text) |
Writing exact text with explicit control over every character. | Writes the supplied string as-is; add "n" yourself if needed. |
print(value, file=f, end="") |
Formatting values conveniently while directing output to a file. | Suppresses print()’s default trailing newline. |
With multiple positional values, print() also inserts a separator between them. Its sep argument controls the text between values, while end controls what follows the final value. For example, use print("Hello", "world", file=f, sep="", end="") to add neither a space nor a newline; use write() if assembling the exact output as one string is clearer. These are separate formatting controls, as shown in the Python documentation for print().
Copy lines without adding extra line endings
When a line read from a file already contains a newline, printing it with the default ending can add a second one. Preserve the line’s existing contents by suppressing print()’s ending:
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →for line in source:
print(line, file=destination, end="")
This avoids adding an extra line ending; it does not remove a newline already present in line. If the source line has no newline, this code does not add one.
Does file.write() add a newline?
No. file.write() writes the characters in the string you pass. If the string ends in "n", that newline is written; otherwise, the method does not add one. This article’s examples use current Python 3 syntax.
What “without a newline” does not mean
Suppressing a newline controls the text written, not when the operating system or storage device physically persists it. A newline-free call does not by itself guarantee that data has been flushed to storage at that moment.
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.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.




