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 Comparisons

.format(first_name)

Why do we have to put in .format(first_name) not just first_name ?

Levi Malinoff
Levi Malinoff
575 Points

There 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!

4 Answers

In 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
Jareth Vasquez
1,744 Points

also, (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)

Just 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
PLUS
youssef b10ta
Courses Plus Student 2,754 Points

you can use as well print(f"Have a great day {first_name}!"}