Python Forum
why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop
#1
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]
Reply
#2
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.
DeaD_EyE and JulyFire like this post
Reply
#3
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  failing to print not matched lines from second file tester_V 13 1,455 Apr-13-2021, 03:36 PM
Last Post: DeaD_EyE
  Pyinstaller 4.2 produced exe containing viruses ? kevpython 3 3,640 Feb-19-2021, 08:55 PM
Last Post: kevpython
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 672 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Unable to print stuff from while loop Nick1507 4 715 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  Creating a variables inside FOR loop zazas321 5 1,732 Sep-16-2020, 04:42 PM
Last Post: Naheed
  Need print out lines before and after the match tester_V 6 1,459 Aug-11-2020, 02:50 AM
Last Post: deanhystad
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 1,775 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Print output in single file using pramika loop deepakkhw 1 804 Jul-11-2020, 11:57 AM
Last Post: j.crater
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 687 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  Unable to combine print statements in for loop adeana 2 731 Jun-12-2020, 05:08 PM
Last Post: adeana

Forum Jump:

User Panel Messages

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