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


Messages In This Thread
why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop - by JulyFire - Jan-09-2021, 11:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 375 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,526 Nov-07-2023, 09:49 AM
Last Post: buran
  Help adding a loop inside a loop Extra 31 4,337 Oct-23-2022, 12:16 AM
Last Post: Extra
  failing to print not matched lines from second file tester_V 14 5,946 Apr-05-2022, 11:56 AM
Last Post: codinglearner
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Pyinstaller 4.2 produced exe containing viruses ? kevpython 3 9,727 Feb-19-2021, 08:55 PM
Last Post: kevpython
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,651 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Unable to print stuff from while loop Nick1507 4 2,280 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  Creating a variables inside FOR loop zazas321 5 4,040 Sep-16-2020, 04:42 PM
Last Post: Naheed
  Need print out lines before and after the match tester_V 6 6,785 Aug-11-2020, 02:50 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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