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!

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 (Retired) Things That Count Things That Count

William Finch
William Finch
251 Points

How would I create a new variable named "summary" that insert "days" into the string "I am {} days old!"?

How would I create the variable.

age = int(21)
days = int(age*52*7)

1 Answer

Hi William,

You'd want to use string interpolation. That is, "evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values." (wikipedia)

In Python, it looks like this:

summary = "I am {} days old!".format(days)

So what's happening here is that Python knows that the '{}' in the summary variable is a placeholder. It fills that holder by calling the .format method with the days variable as an argument.

Hope that helps! Cena

William Finch
William Finch
251 Points

Help out alot. Thank you!