Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mysterious Extra New-Line
#1
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? Huh
Reply
#2
The builtin print function adds a new line by default:
>>> print("\n")


>>> print()

>>> print("\n", end="") # Override the auto "\n" with an empty string ("")

>>> 
Reply
#3
(Feb-27-2019, 10:43 AM)gontajones Wrote: The builtin print function adds a new line by default:

This totally just hit me while I was sitting in a bagel shop eating Doh

Thanks for your response! The ‘end’ parameter for print() will be useful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysterious Indentation Problem Dakodak 11 4,549 Jun-24-2019, 06:23 AM
Last Post: Dakodak
  An Extra 'None' leoahum 5 3,821 Oct-18-2018, 08:20 PM
Last Post: volcano63
  python export to csv writes extra line between rows jahjahcity 4 10,244 Jul-25-2018, 01:36 AM
Last Post: jahjahcity
  csv module writing extra line ChipsSlave 3 2,674 Jun-05-2018, 12:25 PM
Last Post: buran
  Mysterious syntax errors... help? friendlymegalomaniac 3 2,950 Apr-26-2018, 09:22 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020