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 trialBryan Castillo
2,484 Points.format()
He pulled the .format() function out of no where and I just lost it after that. also what is the {} for when he does that line of code towards the end using the .format() function?
1 Answer
Steven Parker
231,271 PointsThe {} symbols are the "placeholder".
Those symbols indicate the place in the template string where the format function will put the argument it is given. For example:
name = "Python"
guess = "{} is fun to use.".format(name)
print(guess) # this will show: "Python is fun to use."
Bryan Castillo
2,484 PointsBryan Castillo
2,484 PointsCan you explain what happened in this line of code from the video? "My favorite flavors are: {}".format(", ".join(flavors))
Steven Parker
231,271 PointsSteven Parker
231,271 PointsOk, I'll break it down into steps:
flavors
is a list of 3 strings:['chocolate', 'mint', 'strawberry']
", ".join(flavors)
this converts the list into one string:'chocolate, mint, strawberry'
"My favorite flavors are: {}".format(
)
this puts the new string in place of the {}'My favorite flavors are: chocolate, mint, strawberry'
so this is the final result