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
159,763 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.