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

oliverchou
oliverchou
20,886 Points

about the format method

when I write: .format(name) does the format method put in the "name" or the variable?

David Dzsotjan
David Dzsotjan
5,929 Points

In this form, it will put the value of the <name> variable in the placeholder, as in

name = 'Kevin'
print('{}'.format(name))  # will output Kevin

To put the word 'name', you gotta type

print('{}'.format('name'))  # will output name

Hope this helps :)

2 Answers

Russell Sawyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Russell Sawyer
Front End Web Development Techdegree Student 15,705 Points

The .format() puts whatever you have in the parentheses inside the place holder in the string represented by the {}. For the challenge you put the variable in the parentheses and the output would be "Treehouse loves Kevin".

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

If you put a string in the parentheses instead of a variable then the result would be "Treehouse loves Russell"

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

Also, you can pass multiple values with multiple place holders. "Treehouse loves Kevin and Russell"

name = 'Kevin'
subject = "Treehouse loves {} and {}".format(name, 'Russell')