Python Python Basics (2015) Python Data Types String Formatting

Cordell Key
Cordell Key
1,130 Points

Help with Python - String Challenge

Why can I not run this code, I believe it may be the syntax?

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

strings.py
name = 'Cordell' 
subject = "Treehouse loves { }" 
print(subject.format(name)) 

4 Answers

Bryan Reed
Bryan Reed
11,746 Points

If you remove the space inside the {} it should work :)

Cordell Key
Cordell Key
1,130 Points

Hi Bryan - Sorry removing the space from {} did not work, but I appreciate the help!

Bryan Reed
Bryan Reed
11,746 Points

Strange, this worked for me:

name = 'Cordell' 
subject = "Treehouse loves {}" 
print(subject.format(name)) 
Mckenzie Hessel
Mckenzie Hessel
Full Stack JavaScript Techdegree Graduate 20,263 Points

Did you figure it out? You can also simplify and make it two lines of code:

name = "Cordell" print("Treehouse loves {}.format(name)")

Arindam Roychowdhury
Arindam Roychowdhury
3,241 Points

"Treehouse loves {name}".format(**{'name':name})

Note the **

>>> name = 'Cordell'
>>> subject = "Treehouse loves { }"
>>> subject.format(**{'name':name})
'Treehouse loves Cordell'
Cordell Key
Cordell Key
1,130 Points

Hi Arindam - Thanks for the help, one the "**" why are these needed? I haven't learned this syntax yet.

Cordell Key
Cordell Key
1,130 Points

I just ran my code in Workspace to see if would run, and it did without any problems.

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