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

I'm trying to use .format in a PYTHON string & keep getting the wrong answer. Can anyone tell me what I'm doing wrong?

I'm trying to format string but keep getting an error.

strings.py
name = "Margaret"
subject = "Treehouse loves "  +  name
status_message = "Treehouse loves  + {}"
"My {} is {}" .format ("name", "Margaret")
print (status_message .format ( name, Margaret )) 
print (status_message)

1 Answer

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

You're overthinking it I think. In a challenge it's important to never do more than they actually ask for. Even if your code is functional (or would be) outside of the challenge, it's possible that it will send back a response to the thing interpreting your code with something they don't expect. I'll show you the code and walk you through it.

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

On the first line we define a variable of "name" and assign your name to it. Then we define a new variable named subject and use string interpolation to assign a new string and format it including your name (which is stored in the name variable). Hope this clears things up a bit!