Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!