Python Python Basics (2015) Python Data Types String Formatting

Hello, Team, I need help with the below task Challenge Task 1 of 2 under python basics topic: strings

Challenge Task 1 of 2 I want to make the email subject one more time, but this time let's use the .format() method for strings.

Go ahead and make your name variable again. Hey, all practice is good practice, right?

strings.py

4 Answers

billy mercier
billy mercier
6,259 Points

it should look like this:

name = 'bob'

subject = 'Treehouse loves {} '.format(name)

print(subject)

or you can:

name = 'bob'

subject = 'Treehouse loves {} '

print(subject.format(name))

I personally prefer first scenario.

subject = 'Treehouse loves {} '.format(name) you don't have to type your name, this 'name' is a variable that you already gave it the string 'irfan' . The function tries to find what type of input is in the ().

If there are no ' ' it searches for the variable that = to a string or value. If it cannot find that variable it causes an error. If you put ' ' then that is a string and you can write anything in it and it will print.

you could do this:

subject = 'Treehouse loves {}'.format('irfan')

however the exercise wants the variable that has your name.

subject = 'Treehouse loves {} '.format(name)

Should it look like this?

name = "Irfan" subject = "Treehouse loves " + name Print(subject.format(Irfan))

Stanley Thijssen
Stanley Thijssen
22,831 Points

Hey Mohammed,

You have to make sure your subject variable accepts the value you pass with the format function, this is done with {} inside your string declaration.

So inside your subject string: subject = "Treehouse loves {}" where {} is the value you pass in with .format(name).

So it should finally look something like this: name = "Irfan" subject = "Treehouse loves {}" Print(subject.format(name))

Thank you guys.