Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Types and Branching If, Else and Elif

Shali Dan
Shali Dan
741 Points

Line 8 and 9: Why do we use .format(first_name) when we can use ("Have a great day", first_name) ?

I don't get why they would use {} .format when they can use first_name. Wouldn't that be easier?

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 67,623 Points

Hey Shali Dan, it seems Craig is attempting to introduce additional ways to format a string.

In the “Have a great day” line, using a simple tuple of arguments fails, since the comma format adds a space between the first_name and the !.

>>> # using comma separation 
>>> first_name = "Craig"
>>> print("Have a great day", first_name, "!")
Have a great day Craig !

>>> # using concatenation works
>>> print("Have a great day " + first_name +"!")
Have a great day Craig!

The latest formatting method f-strings have been optimized to be the fastest method.

Post back if you need more help. Good luck!!!