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 trialBryan Elliott
1,101 PointsExplain like I'm five this line of python code.
first_name = Tammy print("Have a great day {}!".format(first_name))
I'm not sure how this line of code works. I understand that I'm using a method (or function) of print but I'm not getting why it's doing it. I understand that it works but I don't get the logic.
Why wouldn't the code be written like this: print("Have a great day" (first_name)!""
1 Answer
Josh Keenan
20,315 PointsWhilst the results may look the same, the operation is not.
.format()
isn't a method of print()
but of str
(the string object). So what this means is that every string you have, has a set of methods like this one. .format()
let's you update the content of a string by placing something else inside it.
The other approach is concatenation, adding two strings together. There is no difference in performance, most people using Python tend to prefer using format as it's found to be easier on the eyes. You could use either and both will have the same outcome.