Python Forum
why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop - 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: why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop (/thread-31919.html)



why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop - JulyFire - Jan-09-2021

Hi guys, I need help to understand the following codes' logic please. I was trying to print out the keys and values of my dictionaries. My codes did print out the result I desired but I would like to add an empty line in-between each dictionary so I wrote a simple print('\n') located inside my FOR loop. It produced 2 new lines instead of 1. Anyone knows why this happened? Thank you so much for reading this. Greatly appreciated for your help! :)

person_one = {'first_name': 'kevin', 'last_name': 'hart', 'age': 15, 'city': 'kissimmee'}
person_two = {'first_name': 'john', 'last_name': 'smith', 'age': 22, 'city': 'orlando'}
person_three = {'first_name': 'kobe', 'last_name': 'bryant', 'age': 27, 'city': 'tampa'}
people = [person_one, person_two, person_three]

for person in people:
	for name, info in person.items():
		if name != 'age':
			print(f"{name.title()}: {info.title()}") 
		else:
			print(f"{name.title()}: {info}")
	print('\n') #why does this code here produce 2 new lines instead of 1? when I remove this code, no new line is outputted
Output:
First_Name: Kevin Last_Name: Hart Age: 15 City: Kissimmee First_Name: John Last_Name: Smith Age: 22 City: Orlando First_Name: Kobe Last_Name: Bryant Age: 27 City: Tampa [Finished in 0.1s]



RE: why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop - snippsat - Jan-09-2021

Here a hint,so there is parameter in print function you can use to get one new line.
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.



RE: why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop - JulyFire - Jan-10-2021

(Jan-09-2021, 11:34 PM)snippsat Wrote: Here a hint,so there is parameter in print function you can use to get one new line.
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

Ah, I see. So by default, a print() function always end with \n. No wonder if I call a print() function few times in a roll it always start with a new line even though I did not put any \n. Thanks for your help!