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

what am i doing wrong?

?

strings.py
name = "joe"
name.format()
subject = "treehouse loves {}"
subject.format(name)

3 Answers

Hi!

So, what the format method does, is fill the placeholders "{}", with whatever you tell them to. Here is how it works.

# This next statement will assign the string "My name is Eric" to the my_name variable
my_name = "My name is {}".format("Eric") 
# This next statement does the same as the last one, but using the name variable instead of directly 
#putting my name into the statement.
name = "Eric"
my_name = "My name is {}".format(name)

So, following that. What you want to do in this task is use the name variable with the format method to assign the value subject to "Treehouse loves name"

Here it is:

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

hint: For python, "name" is not the same as "Name", so try to make use of that to organize your code.

What does "Name" do?

Steven Parker
Steven Parker
229,744 Points

When the instructions say "Assign this to the variable subject", the "this" they are talking about is the result of using ".format() on the string "Treehouse loves {}" to put your name into the placeholder".

Also, be aware that calling format on a string doesn't modify the string itself, so unless you assign the result to something there's no way to tell that anything happened.

name is the variable you have previously set. In your case, you set it to 'joe', in my case, I set it to 'Eric'. Anyway...

A variable is a place in memory where you store a value.

Hope it helps.