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

Meher Islam
Meher Islam
9,606 Points

Having some issues

I'm trying to create a variable called subject that includes the string "Treehouse loves {}", .format(), and my name variable, which I assigned as "Kiren". When I run the code, I am getting an error message that is saying that Task 1 is no longer running. I'm not sure what that means.

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

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You are very close. format() is a method of str. So the syntax is to add ".format()" at the end of the string:

name = "Kiren"
subject = "Treehouse loves {}".format(name)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Tagging Kenneth Love.

In evaluating this post I tried using named format fields and it was rejected:

name = "Why Not"
# this is rejected
subject = "Treehouse loves {name}".format(name=name)
# so is this
subject = "Treehouse loves {0}".format(name)

The Checker appears to insist on an empty format field {}. Is this overly restrictive?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, I'd say that's too restrictive, Chris Freeman. I've updated the CC.

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

errors in current tasks can cause it to say there are now errors in previous tasks. here you are using format wrong. format is a method and you call it on an object like object.format(arguments). so here the string treehouse...is your object, call format on it, and then the argument to format is the name variable. string.format(variable). the {} in the string are empty, they are a placeholder. the format method will fill them in with the argument to format, name.