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

.format

Cant seem to get this right... i must be over thinking the situation

strings.py
name = "Jamie"
subject = "Treehouse loves.".format()

name = "jamie" subject = "treehouse loves {}".format(name)

This is still giving me a format error :(

3 Answers

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

jammie Hargreaves

Hey so you are missing the {} from your string.

For every set of {} you have inside your string the format method will expect to have a parameter for that.

Example

some_string = "This string should contain {} and {}.".format("peas", "carrots")

This would output:

This string should contain peas and carrots.

Notice how I have TWO sets of {}, so format has two parameters which are String data types, but these could easily be variables that hold another data type.

Another way you can use format is:

some_string = "This string should contain {0} and {1}.".format("peas", "carrots")

The 0 and the 1 are referring to the parameter spots inside format method. You can move these around to change where each item shows up inside the string you are calling format on.

This would output the same:

This string should contain peas and carrots.

or we could also do this and swap the numbers inside the {}

some_string = "This string should contain {1} and {0}.".format("peas", "carrots")

This would now output:

This string should contain carrots and peas.

Very close.

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

Notice I only changed a couple of things:

  • copy/paste the string they had given you, including the {}. You had excluded it and used a period.
  • I put the String "Treehouse loves {}" before the .format() and used the variable name inside format so it knew to replace {} with the variable name.

Resolved. Thank you all