Each row ends with a newline character ('\n'
).
When iterating over a Python file,
this character remains at the end of the string line
¹.
The print()
function then adds another newline character.
This function always ends the line.
You can suppress it using the argument end=''
).
That is one way how to "fix" this extra spacing.
The other is to use the method rstrip()
for each line.
This method removes all spaces and newline characters
that are the end of the string.
¹ Why does Python keep the newline character? If the '\n'
at the end of line was removed,
it would not be possible to know if the last line ended with '\n'
or not.