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 trialjammie Hargreaves
Courses Plus Student 2,455 Points.format
Cant seem to get this right... i must be over thinking the situation
name = "Jamie"
subject = "Treehouse loves.".format()
3 Answers
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsHey 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.
Philip Gales
15,193 PointsVery 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.
jammie Hargreaves
Courses Plus Student 2,455 PointsResolved. Thank you all
jammie Hargreaves
Courses Plus Student 2,455 Pointsjammie Hargreaves
Courses Plus Student 2,455 Pointsname = "jamie" subject = "treehouse loves {}".format(name)
This is still giving me a format error :(