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 (2015) Python Data Types String Formatting

Alisa Tashiro
Alisa Tashiro
724 Points

What is wrong with my code? (Python) Using .format and {}

Hi! I'm new to Python and I'm having some trouble with this. I was wondering what I need to do differently on my code. name = "Alisa" subject = "Treehouse loves {}" print(subject.format(name))

1 Answer

Ryan Ruscett
Ryan Ruscett
23,309 Points

Yo,

Welcome to Python, you will be a Pythonista in no time. Let's get to it.

Here is your code:

name = "Alisa" 
subject = "Treehouse loves {}" 
print(subject.format(name))

I will give you credit for the ol' college try. But let's look a this for a second.

  1. Ok name is equal to alias. Ok cool that looks right.
  2. subject equals a string "Treehouse loves {}" Ok this is ok
  3. BUT you then try to print(subject.format(name)) <---- This is where you went wrong.

I get your logic. Subject is a string. Then you are saying print format this string with the variable my name instead of {}. BUUUT that's not really what is happening.

name = "Alisa" 
subject = "Treehouse loves {}".format(name) 

Now format is a string method. So we can take the raw stirng "Treehouse loves {}" and add a .format(name) to it. This is read like this.

  1. Here is a string. Oh but it has a .format after it.
  2. Let's grab the variable passed into .format and in this case it's name.
  3. Let's now look at the raw string and find any {}. We will then insert the variable name in the first instance of {} within the string.

There is only 1 {} in the string. There is also only 1 value passed into format. So the first {} gets the first value passed into format which is name.

If I had "Treehouse Loves {} and {}".format(name, someOtherVariable)

Python would say ok. There is a .format. Let's take the first variable passed into .format which is name. And put it in the first instance of {} (left to right) inside the string. Oh but wait there is another variable passed into .format called someOtherVariable. Ok that will go into the second instance of {}.

This is all decided on the fly. Your code says that subject is equal to a string. Then in a separate function called print you try to do the substitution. You can't do that. take this for example.

return 10 + 5

It will return 15. BUT

return 10
10 + 15

The return statement will process the code then return the value. Once it's returned it can't be changed. You are doing the same type of thing.

subject = subject = "Treehouse loves {}"
print(subject.format(name))

you already assigned the string to subject. You are now saying that subject should be changed when it's printed. You have to change subject all at once. Not in two different statements.

Does this make any sense? I may have lost myself in all of this lol. If not please let me know and I can try to explain it again.

Let me know if this makes any sense to you, since i may have lost myself in my explanation lol. If you don't get it let me know and I can re-work it.

Thanks!

Alisa Tashiro
Alisa Tashiro
724 Points

Thank you so much Ryan! I now understand that I need to add the .format to the raw string (:

I tried something similar on my python terminal,

name = "Alisa" subject = "Treehouse loves {}" subject.format(name)

and that works. So I can't add a print statement because I'm not supposed to change the variable, subject, within the print statement. Is that right? Please feel free to correct me if I'm not!!

Ryan Ruscett
Ryan Ruscett
23,309 Points

Well, not entirely.

x = 1
y = 2
print(x + y

This will return 3 which is the answer.

The idea here is that print is evaluating what it has to print before printing it. It's doing all the work first. Doing all the work at 1 time.

Your code

name = "Alisa" 
subject = "Treehouse loves {}" 
print(subject.format(name))

The .format says how many arguments do I have. In this instance just 1 "name". It then says ok let me find the first instance of {} and fill it in with the "name" value. IMPORTANT distinction is it's not yet a string. It's doing the work of making a string first.

When this is performed in a single statement. The {} get's substituted for the name BEFORE the value becomes a string. It does the substitution and then makes the string.

You are making a string that is simply "Treehouse loves {}". In the second statement the .format looks back and says ok let me find {} and it can't . All it sees is a string. It doesn't see a {}. Since .format sees {} before the string is ever made. It does the substitution in order to make the string. So there for it doesn't fill in the {} with the variable. for {} doesn't fit into a string.

Does that help?

Does that make sense?

Alisa Tashiro
Alisa Tashiro
724 Points

OH!!! That makes complete sense! Thank you so much Ryan for elaborating it more for me(: Definitely helped me a lot!

Ryan Ruscett
Ryan Ruscett
23,309 Points

That's awesome. I know forums are a great place to get help but it's not always helpful to use them as a resource for understanding. So if you understood what I was saying, that's sweet.

Good Luck!