Unintended Space In New Line [Solved]

Hello,

I have a question regarding the added one character indentation when using the newline character. Why does the newline start from the position of the 2nd character versus the first?

unwanted_space

How can this be mitigated?

Use print('\n', 5, sep=''). The default is using print('\n', 5, sep=' ').

2 Likes

This is from the default sep=' ' in print.

Try print('\n', 5, sep='')

edit: dang, too slow! :slightly_smiling_face:

1 Like

Thank you both guys. I appreciate it. :call_me_hand:

Wonder why this is not the default.

I suppose more often people would like to see the elements of tuples separated a bit

>>> print(2,3,5,7)
2 3 5 7

instead of all clumped together

>>> print(2,3,5,7, sep='')
2357

Well, this appears to affect the spacing between results. The newline starting position appears to be independent of this. You can keep the spaces, just start from the same starting point.

If you want to print one result per line, use sep='\n' and don’t add the newlines yourself.

>>> print(1, 2, 3, 4, sep="\n")
1
2
3
4

I’m not sure what you mean by “The newline starting position appears to be independent”. You’re printing a literal newline, then a sep (space by default), then the next item. It’s giving you exactly what you asked for.

1 Like

Thank you. I will just make a note to specifically add and edit the sep operator in my print statements.

Much obliged. :handshake:

Even better, use one print per output line, and use f-strings for tight control of formatting:

print()  # blank line
print(f"{value1}, {value2:.2f}")

Obviously this is a subjective thing, but I personally find this approach the cleanest.

2 Likes

Yes, this is an alternate approach. I would like to avoid additional print statements, however. My intentions are to have multiple outputs, each starting on a new line, and starting from the first position.

Thank you for providing a potential approach.

Another way of doing it is print(f"\n5")