Mysterious Extra New-Line - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Mysterious Extra New-Line (/thread-16414.html) |
Mysterious Extra New-Line - trevorkavanaugh - Feb-27-2019 Hey, So I am doing exercises out of a Python book I am reading and for some reason this code is printing two new-lines between "- turkey" and "- bacon" and I was hoping someone could explain to me why this is: def order_sandwich(*items): for item in items: print("- " + item) order_sandwich('turkey') print("\n") order_sandwich('bacon', 'lettuce', 'tomato')When I write it like this, I only get one new-line, which is what I was expecting from the first snippet: def order_sandwich(*items): for item in items: print("- " + item) order_sandwich('turkey\n') order_sandwich('bacon', 'lettuce', 'tomato')What am I missing? RE: Mysterious Extra New-Line - gontajones - Feb-27-2019 The builtin print function adds a new line by default:>>> print("\n") >>> print() >>> print("\n", end="") # Override the auto "\n" with an empty string ("") >>> RE: Mysterious Extra New-Line - trevorkavanaugh - Feb-27-2019 (Feb-27-2019, 10:43 AM)gontajones Wrote: The builtin This totally just hit me while I was sitting in a bagel shop eating Thanks for your response! The ‘end’ parameter for print() will be useful. |