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

Harsh Kumar Woike
Harsh Kumar Woike
204 Points

help

how to make a variable named summary and insert "days" in the string "i am {} days old"

2 Answers

Hi Harsh

you can use the format method to replace {} for your variable. say you had a variable named age with your age and you wanted to insert it into a string like so "My age is {}!", you can call the format method on the string passing in your variable age as the parameter. The more the placeholders the more the parameters you would have to pass in to the format method.

example 1
age=35
"My age is {}!".format(age)

example2
age=35
noOfCars = 5

"My age is {} and I drive {} cars!".format(age,noOfCars)
Harsh Kumar Woike
Harsh Kumar Woike
204 Points

thanks for the reply. but maybe i didnt explain myself correctly. I have been given 3 tasks in python basics 'things that count' section. the three tasks are: 1) Create a variable named 'age' that contains your age. (i have completed this task) 2) Now, create a variable named 'days' that is your age multiplied by 52 and multiplied by 7. (i completed this too.) 3) Finally, create a new variable named 'summary' that inserts days into the string "I am {days} days old!" I am not able to complete the 3 task which asks me to create a new variable named 'summary'. please help. thanks.

Hi Harsh

I understood what you were asking. I wanted to explain and give you example so you could complete the task on your own.

# this is how you do it
age = 35
days = age *52 * 7 
summary = "I am {} days old!".format(days)
Harsh Kumar Woike
Harsh Kumar Woike
204 Points

Now that i re-read your first answer, i understand that you wanted me to complete it on my own. i appreciate it thanks. the explanation was easy.

Harsh Kumar Woike
Harsh Kumar Woike
204 Points

Now that i re-read your first answer, i understand that you wanted me to complete it on my own. i appreciate it thanks. the explanation was easy.