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

I'm formatting strings like so: subject = "Treehouse loves {}" print(subject.format(name)) it works in the console

I'm formatting strings like so: subject = "Treehouse loves {}" print(subject.format(name)) works in the console but not in the lesson...

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

2 Answers

Stéphane Diez
Stéphane Diez
19,350 Points

When you use {} in a string, you need to format that with the format() function.

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

yes, that's how I finally solved it... but it works both ways...

subject="blah blah blah {}" subject.format(name)

is the same as

subject="blah blah blah {}".format(name)

isn't it?

Stéphane Diez
Stéphane Diez
19,350 Points

Yes it's the same, but it's better to use .format() after the string.