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

String Formatting

This is a very simple problem, but I am not sure what I am doing wrong. The code challenge asks me to create a string, label it subject, and then use .format() in order to change what is in the curly brace. I keep getting errors though.

strings.py
name = "Antonio"
myString = "Treehouse loves {}"
subject = myString.format(name)

3 Answers

Travis Bailey
Travis Bailey
13,675 Points

Technically your answer is correct, but it might be a bug in the way the challenge is checking the answer. Instead of breaking your answer into three lines, try doing it in two. There's no need to store 'myString' as you can perform what you do in lines 2 & 3 in one line.

For example:

name = 'Arthas'
subject = 'Treehouse loves {}'.format(name)

Again, while your answer is technically correct I think it's more common to see string formatting in the example I gave above.

Thanks guys, that worked. I did not know that it could be written in just 2 lines of code like that. Greatly appreciated.

The first question asks you to create the name variable so

name="Shawn"

The second question asks OK, so now use .format() on the string "Treehouse loves {}" to put your name into the placeholder. Assign this to the variable subject again.

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

Hope that helps

Melanie Villela
Melanie Villela
3,048 Points

I have a question. Why wouldn't this work:

my screenshot/ image link