That's because the last char of line
is \n
aka a newline.
To prevent that from happening, do a
print(line, end='')
The default end
is \n
so by default, you add two new lines (one in the string, and one at the end of the print).
That means that the solution I provided above is equivalent to
print(line[:-1])
You can also remove all newlines by doing
print(line.replace('\n', ''))
Remove all trailing whitespaces (including newlines)
print(line.strip())
Remove all trailing whitespaces (including newlines) at the end
print(line.rstrip())
Remove all trailing whitespaces (including newlines) at the beginning
print(line.lstrip())