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 trialEmery Taber
507 Points.format(first_name)
Why do we have to put in .format(first_name) not just first_name ?
4 Answers
adrian miranda
13,561 PointsIn general, similar things will happen, but using .format causes something special to happen. If you say:
print("Hello {}!".format(first_name))
The call to format will cause it to create a new string, which has been formatted in the way you requested. While you could just do:
print("Hello", firstname + "!")
And it will do the same thing, it is not as flexible. The second method is handing a list to the call to print, and it will print each thing in order, with a space between each one.
By using .format, we can do more complex things, with more control over what happens. For a silly example, print(a,b)
will always put a space between a and b. But by using format, we can control if the space is there. And it you look into the manual, you'll see you can do more complicated things as well.
Jareth Vasquez
1,744 Pointsalso, (please correct me if im wrong) wouldnt
print(first_name + "!")
also make all future first name calls have the "!"? (just started today so please forgive the n00b post)
adrian miranda
13,561 PointsJust calling:
print(first_name + "!")
Will not change the value of first_name
. If you wanted to update the value of first_name, you would have to do something like:
first_name = first_name + "!"
But if you just do the print, you are not actually setting a new value for first_name.
youssef b10ta
Courses Plus Student 2,755 Pointsyou can use as well print(f"Have a great day {first_name}!"}
Levi Malinoff
575 PointsLevi Malinoff
575 PointsThere is no effective difference in output between line 13 as:
print("Have a great day {}!".format(first_name))
and
print("Have a great day", first_name + "!")
. The first one may be a little better aesthetically, but that's it. Hope this helps!