Python Print Without Extra Newline When Reading a File

Problem Formulation

Given a file with name file.txt and the following content:

hi
finxters!
:)

You created a Python script that reads the file line-by-line and prints each line to the shell like so:

with open("file.txt") as file:
    for line in file:
        print(line)

But the output unexpectedly adds an extra newline to each line so you obtain the following output on the Python shell:

hi

finxters!

:)

How to print without the extra newlines?

Method 1: print() with end=” Argument

Explanation: Python’s built-in print() function takes an optional end argument—per default set to the newline character: end='\n'. Consequently, the expression print(line) adds a newline from the print statement in addition to the newline read from the file and stored in the variable line.

The solution is to get rid of one of the newline characters, for example by printing without newline by setting the argument end=''.

with open("file.txt") as file:
    for line in file:
        print(line, end = '')

The output now correctly displays the file content:

hi
finxters!
:)

You can learn more about the print() built-in function in the following video:

Method 2: Remove Newline From String with rstrip()

When printing a line read from a file, Python prints one newline character from the file and one from the print statement. You can get rid of the newline character in the line by using the line.rstrip() method that removes all whitespaces from the end of the line.

with open("file.txt") as file:
    for line in file:
        print(line.rstrip())

Again, the output now correctly displays the file content with only one newline character between the lines:

hi
finxters!
:)

The following video gives a short intro to the string.rstrip() method—feel free to watch!

Technically, using string.rstrip() removes all whitespaces from the string—even tabular characters '\t' and empty spaces ' '. If this is not what you want, use the following simple method:

with open("file.txt") as file:
    for line in file:
        print(line.rstrip('\n'))

This will only remove the trailing newline character and not the remaining whitespaces. A (worse) alternative is given next for comprehensibility:

Method 3: Remove Newline From String with Slicing

When reading a file line by line, every line has a trailing newline character. This is the last character of the line string. To remove the last character from the string line, use the slicing operation line[:-1] with negative index -1. This reliably removes the last character which is the newline character '\n' and avoids printing the newline twice.

But the last line in a file doesn’t have a newline character. In that case, to avoid removing the last regular character, you need to print the last line as is. You can do this with a simple if-else block like so:

with open("file.txt") as file:
    for line in file:
        if line[-1] == '\n':
            print(line[:-1])
        else:
            print(line)

However, this solution is not as clean and Pythonic as the previous solutions. It’s a good exercise on an important Python concept, slicing, so I decided to include it anyways. You can master slicing here:

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!