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

Calum Hill
Calum Hill
4,847 Points

Not sure how to get around this, but this exercise seems to not want to work however I try it.

name = "Calum" message = "Treehouse loves {}" subject = message.format(name) print(subject) (Doesn't work without the print() either)

I don't understand why this isn't working, works fine in terminal.

I couldn't get subject = str("Treehouse loves {}").format(name) to work either.

nor did: subject = str("Treehouse loves {}").format("Calum")

Don't really know what else to do here.

strings.py
name = "Calum"
message = "Treehouse loves {}"
subject = subject.format(name)

print(subject)
Calum Hill
Calum Hill
4,847 Points

subject should be message within the subject var, but either way, still doesn't work.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I feel like you're overthinking this a bit. First, you're never asked to print anything. It's important in challenges to try not to do anything that the challenge doesn't explicitly ask for. Even if functional, this can cause the challenge to fail. Also, you've set up a new variable named message which isn't asked for. You've also tried using the str() method which is also unnecessary. Take a look at how I did it:

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

Here I create two variables. One named name and one named subject. I assign the string literal of my name to the name variable. Now in the subject variable, we're using what's called string interpolation. The format method is going to take the value of whatever variable we put in there and put it in that spot in the string. The end result will be that if we were now to print out subject we would see "Treehouse loves Jen".

Hope this helps! :sparkles: