🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

write()

AI & DATA SCIENCE // write

file.write() writes a string (or bytes, in binary mode) to an open file, returning the number of characters (or bytes) written, without automatically adding a newline.

Syntax

f.write(text)

Deep Dive Course

Unlike print(), write() does not add a trailing newline automatically — if you want each write() call to start a new line, you need to include a newline character explicitly in the string. write() requires the file to be opened in a writable mode, and writing to a file opened in text mode requires a str argument, while a file opened in binary mode requires a bytes argument instead — mixing the two raises a TypeError. Writes may be buffered in memory rather than immediately hitting disk, until the file is closed or explicitly flushed.

1Understanding write()

Unlike print(), write() does not add a trailing newline automatically — if you want each write() call to start a new line, you need to include a newline character explicitly in the string. write() requires the file to be opened in a writable mode, and writing to a file opened in text mode requires a str argument, while a file opened in binary mode requires a bytes argument instead — mixing the two raises a TypeError. Writes may be buffered in memory rather than immediately hitting disk, until the file is closed or explicitly flushed.

💡

Remember write() doesn't add a newline like print() does — if you're writing multiple lines in a loop, append a newline character to each string yourself, or your output will run together on one line.

editor.html
with open("output.txt", "w") as f:
    f.write("Hello")
    f.write(", World!")

with open("output.txt", "r") as f:
    print(f.read())
localhost:3000

2Practical Example

Here is a real-world application of write() showing how it is used in production Python code.

editor.html
lines = ["apple", "banana", "cherry"]
with open("fruits.txt", "w") as f:
    for fruit in lines:
        f.write(fruit + "\n")

with open("fruits.txt", "r") as f:
    print(f.read())
localhost:3000

3Best Practices

Follow these guidelines when working with write():

1. Add a newline character explicitly to strings passed to write() when each one should start a new line

2. Use a with block so writes are properly flushed and the file is closed automatically when you're done, rather than relying on manual flush()/close() calls

3. Match the file's mode to your data: text mode with str for write(), binary mode with bytes

⚠️

Tip: Remember write() doesn't add a newline like print() does — if you're writing multiple lines in a loop, append a newline character to each string yourself, or your output will run together on one line.

editor.html
with open("output.txt", "w") as f:
    f.write("Hello")
    f.write(", World!")

with open("output.txt", "r") as f:
    print(f.read())
localhost:3000

Examples

Example 01Basic Usage
with open("output.txt", "w") as f:
    f.write("Hello")
    f.write(", World!")

with open("output.txt", "r") as f:
    print(f.read())
Example 02Advanced Example
lines = ["apple", "banana", "cherry"]
with open("fruits.txt", "w") as f:
    for fruit in lines:
        f.write(fruit + "\n")

with open("fruits.txt", "r") as f:
    print(f.read())

Best Practices

  • Add a newline character explicitly to strings passed to write() when each one should start a new line
  • Use a with block so writes are properly flushed and the file is closed automatically when you're done, rather than relying on manual flush()/close() calls
  • Match the file's mode to your data: text mode with str for write(), binary mode with bytes

Interview Question

Why doesn't calling write() twice in a row automatically put its two strings on separate lines in the file?

Hint: Compare write()'s behavior to print()'s.

Unlike print(), which appends a newline character after its output by default, write() writes exactly the string you give it and nothing more, with no implicit newline added. Two consecutive write() calls without an explicit newline character in either string simply get concatenated together on the same line in the file, which is why line-by-line file writing requires adding a newline to each string manually.

Exercises

MediumPractice using write() in a real scenario.
View Solution
with open("output.txt", "w") as f:
    f.write("Hello")
    f.write(", World!")

with open("output.txt", "r") as f:
    print(f.read())

Frequently Asked Questions

Why doesn't calling write() twice in a row automatically put its two strings on separate lines in the file?

Unlike print(), which appends a newline character after its output by default, write() writes exactly the string you give it and nothing more, with no implicit newline added. Two consecutive write() calls without an explicit newline character in either string simply get concatenated together on the same line in the file, which is why line-by-line file writing requires adding a newline to each string manually.

Related Functions

read()open()with-statement