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! While you're at it, check out some resources Treehouse students have shared here.

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 (2015) Python Data Types String Formatting

I can't do task strings.py correctly without using print() . Can you show me how to do it please?

I attached the code i wrote

strings.py
name = "abdallah"
subject.format= "Treehouse loves {}"
subject.format(name)

5 Answers

Ok, there are objects and there are functions. Comparing to real world, Objects are data or entity and functions are the things they do or things they are used for.

Similarly, variable here holds data.

name="Tom"
address="324, baker street"
money=72932329473

And functions would be something like:

def getMyName():
    return name
def getMyMoney():
    return money
def doubleMyMoney():
    money = money*2

Here, name and subjects holds the data/information and we use format() function to format the information in certain way.

To access a function over the data, . dot operator is used.

here...

name = "abdallah"
#it should be subject, that holds some information. Where as .format is the function.
#by doing subject.format= "Treehouse loves {}" you are telling that-for format function of subject, give that value.
subject= "Treehouse loves {}"
subject.format(name)

Do comment if its not clear.

The one which worked is your next comment. Thanks!

Dear Krishna,

Thanks for your feed back, this was the challange " OK, now use .format() on the string "Treehouse loves {}" to put your name into the placeholder. Assign this to the variable subject (so start with subject =).

You do not need to print() anything."

and my code was:

name = "abdallah" subject = "Treehouse loves {}" subject = subject.format(name)

it gives me this error: "Bummer! Be sure to use the {} placeholder and the .format() method."

Is this an issue with the website itself?

Thanks, Abdallah

Well, as the question says that start with subject=

So what author wants is, for you to apply the format() function right away while assigning the value.

name = "abdallah"
#it should be subject, that holds some information. Where as .format is the function.
#by doing subject.format= "Treehouse loves {}" you are telling that-for format function of subject, give that value.
subject= "Treehouse loves {}".format(name)

If its any consolation i think, the language of question has been changed to somewhat better wording.

When i tried, i had similar problem.

Here you go!

name = "abdallah"
subject = "Treehouse loves"
print("{} {}".format(subject, name))

This will print out "Treehouse loves abdallah".

Hi Marcus,

Thanks for your reply but it didn't work !. Did you notice they said in the task "You do not need to print() anything."?

This is the trick i can't get solution till now.

Thanks, Abdallah

Excellent Krishna, that worked out!

Thanks again and sorry i didn't see your last comment in the first time.