October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Write to a File Without a Newline in Python

Python’s file.write() does not add a newline. Learn when to use it, how print(..., end="") works, and how to avoid extra line endings when copying lines.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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().

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.