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

Leonardo Reyes
Leonardo Reyes
216 Points

{} vs directly calling

Towards the end of the If, Else, Elif video Craig prints...

print("Have a great day {}!".format(first_name))

How come he didn't print...

print("Have a great day", first_name, "!")

Are there times where one is more convenient than the other?

1 Answer

Steven Parker
Steven Parker
231,275 Points

It's possible that the choice was made because providing multiple arguments to "print" may not have been covered in the course at that point. Or it could be that multiple arguments aren't as widely supported in different (or older) versions of Python. Or it might have just been out of habit.

Rather than either of those methods, I prefer to use the newer formatted string literals (also known as "F-strings"):

print(f"Have a great day {first_name}!")

But there's likely more than one tool in a programmer's toolkit that can do a job, and the choice is often just a matter of programmer preference.