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 Meet Python Create a Variable

Whats wrong with this line?

I'm pretty new and I just need a little help with why I keep getting Syntax Error.

using_variables.py
(favorite_color)="blue"
print("the color "favorite color" is my favorite")

You can't do that in python. You would need to use format or the + symbol.

Using Format:

(favorite_color)="blue"
print("the color {} is my favorite".format(favorite_color))

Using + symbol:

(favorite_color)="blue"
print('the color ' + favorite_color + ' is my favorite')

Hope this helps.

2 Answers

Antonio De Rose
Antonio De Rose
20,885 Points
(favorite_color)="blue" #cant have a bracket to assign a variable
print("the color "favorite color" is my favorite") # where are the commas to seperate a variable to string, there should be 2 commas, and the variable name assigned at the top differs to one is been used.
Curtis Vanzandt
Curtis Vanzandt
9,165 Points

Two things I see here:

(favorite_color)="blue"
#  Don't necessarily know if this is the main issue, but there's no need to wrap the variable name in parentheses.

print("the color "favorite color" is my favorite")
#  This is likely your main issue. You aren't concatenating this string correctly. Correct ways to concatenate this would be:
print("The color " + favorite_color + " is my favorite")
print("The color", favorite_color, "is my favorite")
print("The color {} is my favorite".format(favorite_color))
print("The color %s is my favorite" % favorite_color)

Hope this helps!