Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEnrica Fedeli-Jaques
6,773 Pointswhy do we use first_name sometimes and {} with .format some other?
Hi, I'm not sure I understand why we sometimes choose to use the variable first_name some times and the place holder {} some others... isn't the outcome the same?
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThere are four styles of print statements. Which to use can be due to preference at the time. Both forms used in the video are valid.
The f-string and βold styleβ % formatting can be seen in the doc.
Post back if you need more help. Good luck!!!
Stephen Holmes
2,439 PointsApologies. I think I've just worked out the reason why!
"The brackets and characters within them (called format fields) are replaced with the objects passed into the str.format() method. A number in the brackets can be used to refer to the position of the object passed into the str.format() method."
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"
:D
Stephen Holmes
2,439 PointsHello from England!
I understand that the {} are placeholders but don't understand the use of .format(first_name) on line 8 & 9. As would it not just fill in with {}?
first_name = input("What is your first name?")
print("Hello,", first_name)
if first_name == "Craig":
print(first_name, "is learning Python")
elif first_name == "Maximmiane":
print(first_name, "is learning with fellow students in the Community! Me too1")
else:
print("You should totally learn Python, {}!".format(first_name))
print("Have a great day {}!".format(first_name))
Enrica Fedeli-Jaques
6,773 PointsEnrica Fedeli-Jaques
6,773 PointsOh ok, so they are pretty much interchangeable I guess, and every time we assess which is better for the specific situation. Thank you for your answer, the wishes and the link. :)