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 trial

Python Python Basics Types and Branching If, Else and Elif

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,773 Points

why 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
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

There 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!!!

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,773 Points

Oh 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. :)

Apologies. 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

Hello 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))