Reputation: 3437
I have a list of lines (from a text file) to be printed in terminal. This is the format I like (without empty lines between printed lines):
>>> print("line \n" * 5)
line
line
line
line
line
This is how it looks (a line with text, an empty line, etc.):
>>> for x in range(0, 5): print "line \n"
...
line
line
line
line
line
Any idea why it's working in this way and how to print my list of lines without empty lines between them?
UPDATE (this is where I want to get rid of empty lines):
with open(FILE, 'r') as f:
lines = f.readlines()
for line in lines:
print line
Upvotes: 0
Views: 7922
Reputation: 362756
This is actually due to the way you're reading the file. If you've done something like
with open(FILE) as f:
data = f.readlines()
Then the data
will be a list of lines which retain the end of line character. If you don't want the end of line character, use this instead:
with open(FILE) as f:
data = f.read().splitlines()
If you don't want to change the way that you read the data into a list, then concatenate and print the lines like this instead:
print ''.join(data)
To answer the literal question, if you have a string with a newline, and you want to suppress the newline generated by the print statement, the way to do that in Python 2 is by using a trailing comma on the print statement:
with open(FILE) as f:
for line in f:
print line,
But note, that's not really necessary to iterate line by line like that. You may as well just print the data directly:
with open(FILE) as f:
print f.read()
Upvotes: 2
Reputation: 20067
By default print
adds a newline, and your strings also have it; you have two possible solutions.
Either override this behaviour, setting the string to be put to empty:
# python 3.*
for x in range(0, 5): print("line \n", end="")
And
# python 2.*
from __future__ import print_function # to use py3 print in py2
for x in range(0, 5): print("line \n", end="")
Alternatively, if you don't have full control over the lines - some may have the newlines, other - not, trim any newline endings and leave the default behaviour:
for x in range(0, 5): print("line \n".rstrip("\n").rstrip("\r")) # takes care of also Windows line ending, just in case
Upvotes: 1
Reputation: 28380
A standard call to the print function will add a newline automatically so you can either remove the \n
from your existing line(s), if the lines come from an external file use line.strip()
, or tell print not to add the newline with: print(line, end='')
This will work for Python3 or for 2.7 with from __future__ import print_function
as the first code line.
Upvotes: 0
Reputation: 2497
print
adds a trailing newline by default. You can disable it as follows:
from __future__ import print_function # only needed for python 2
print("string", end="")
Upvotes: 2
Reputation: 12341
The print command implicitly adds a new line feed \n
in the output display. Remove the \n
to avoid printing blank lines in the looping structure.
Upvotes: 1
Reputation: 1363
When using a for loop you can remove "\n" as follows
for x in range(0, 5): print "line"
Upvotes: 1