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 Using Input

favorite_color = input("What is your favorite color?") print("The color {} is a great color!".format(favorite_color))

Why did the above code work? Why do I use curly brackets and the .format. Note*this code was copied from another post.

1 Answer

Timo Särkkä
PLUS
Timo Särkkä
Courses Plus Student 5,419 Points

Hi Mustafa.

It's called string formatting. The code snippet you provided will let you ask input from the user and store it in a variable (favorite_color in this case). You can then neatly tell python where to place the variable inside your string by using the curly brackets as a placeholder. Using the .format will then actually place the value of the variable inside the string.

You could also place the variable inside the string using concatenation, for example, but it could make the code look messy. Especially when you do this for several variables inside one string.

Hope this helps!