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

bennett miller
bennett miller
357 Points

I am trying to figure this out but every time I write the second line of code suddenly my "task 1" stops working

basically i am supposed to use the .format method to add a variable called subject to combine two strings "Treehouse loves" and the name of my choice however each time i enter the name it says task one no longer passes

strings.py
name = "{}"
subject = "Treehouse loves"+name.format(charlie)

2 Answers

Steven Parker
Steven Parker
229,783 Points

That can be confusing until you understand that any syntax error also invalidates the re-validations of previous tasks.

In this case the syntax error is caused by an undefined reference to "charlie". But that should have been assigned as a string literal to the variable "name" in task 1. Apparently you were able to pass task 1 because the system accepted "{}" as your name!

Then, when you get to task 2, the entire string "Treehouse loves {}" should be used as the template for the "format", and the argument should be the variable you created in task 1.

Give it another shot, I'll bet you can get it now.

AJ Salmon
AJ Salmon
5,675 Points

Hey Bennet,

When using the .format() method on a string, the string being formatted needs to contain the curly braces ({}) within the quotation marks, and then in the parentheses after format(), you pass it an argument that will be inserted into the string wherever those curly braces are placed. An example would look like this:

>>> my_name = "Aaron"
>>> my_example = "My name is {}".format(my_name)

If we printed my_example, we'd get the string "My name is Aaron"

This challenge wants you to first make a 'name' variable that is assigned to a string, preferably containing your name, but it can be any string you like. Then, you're supposed to make a new variable, subject, that is assigned to the string "Treehouse loves {}". After the closing quotes, use format() and pass it your name variable (like how my_name was passed to format() in the example). This will insert the contents of 'name' into the "Treehouse loves {}" string where the curly braces are. Hope this helps!

-AJ

bennett miller
bennett miller
357 Points

Thanks a lot I think I must have misunderstood something there