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

nazia zaman
nazia zaman
1,410 Points

summary part of changing days into string

How do I do the last part of summary = "I am {} days old!" I changed it to "I am {} days old!".format(days) "I am {} days old!".format(str=days) and a few other ways but to no use. Can anyone help out? thanks!

days_alive.py
age = 27
days = age * 52 * 7
summary = "I am {} days old!"

2 Answers

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

It is about formatting string.

There are several ways to do this but what you are taught is probably using format() function.

It works as follow.

The placeholder in a string will be substituted by arguments in format() function.

Simplest placeholder is pair of bracket {}. Using placeholder in this way makes it positional placeholder.

The number of argument has to match with number of placeholder (in simplest form)

For example,

my_string = '{} {}'
a = hello
b = world

my_string.format(a, b) # ''hello world"

So to apply this logic to question.

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

You said you did "I am {} days old!".format(days) there is no reason why this shouldn't work.

nazia zaman
nazia zaman
1,410 Points

I will try again and see. But i'm fairly certain I did this a few times and it didn't work.

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Here's my passed code while trying to solve your issue.

age = 27
days = age * 52 * 7
summary = "I am {} days old!".format(days)

If the question didn't pass please let me know what the grader said.

nazia zaman
nazia zaman
1,410 Points

Gunhoo Yoon, it worked! thanks so much! :D Although i have no idea why it didn't pass the first time I tried. This is the most basic thing to come to mind and I know i did it! So weird but thanks for saving the day for me! haha :)

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

It could be small typo or connection issue but you get the idea that's all it matters.