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

Damian McCarthy
Damian McCarthy
3,656 Points

I dont get this!!!

Am I supposed to be inserting the string into the other string like this...

strings.py
name = format("Damian")
subject = "Tree house loves {name}"

The challenge is asking to put your name as a string in a variable. Then, use the format() method to let python replace the placeholder curly brackets {} you used in the the subject variable. Here's an example of the format() method in use.

town  = "Portland"

future = "I would like to live in {}".format(town) 

The format() method will replace the empty curly braces {} with what you put between the parentheses (). In this case, I passed the variable town into format() method beings used in the variable named future. So, if you were to print the variable future, it would ultimately read "I would like to live in Portland".

I hope this helps!

1 Answer

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

Hi Damian, Not exactly. So the .format()is a function that modifies strings. I think your confusion is rooted in a simple lack of understanding in syntax (which is easily fixed)! The challenge asks you to create a variable called name whose value is a string.

name = 'nicole'

Then you're asked to modify the string "Treehouse loves {}" with the .format() function, and store this result in a variable named subject. Remember the {} indicate a placeholder in which the contents of .format() will be inserted.

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

If you were to take a look at subject, you'd see it's value is:

>>>subject
>>>"Treehouse loves nicole"

I hope this helps :)