pythonprintingindentationauto-indent

How to print newline in Python without an automatic indent in the next line?


I was incorporating the \n command into my code to print the next part onto the next time. While the original goal was achieved, there was an unexpected indent only for the first line of the result that started on the newline.

print("Rank:\n", rank_desc)

That results in:

Rank:
 count   500.000000
mean   250.500000
std    144.4181833
min      1.000000
etc...

One way to solve the issue is to just have two print statements. However, now matter how I sliced and mixed the \n in my code, I could not get the indent to go away.


Solution

  • use sep parameter of print for this:

    # sep stands for separator
    print("Rank:", rank_desc, sep='\n')