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

Why use brackets instead of variables?

In a lot of demonstration code I see print statements formatted with brackets in for variables and .format at the end to fill in the variables. Why don't most people just use the variable names in place of the brackets?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

There is a good discussion of this on StackOverflow

Basically, using one of the format method (%s, .format(), f-strings) provides readability for complex substitutions, and automatic conversion to type str. And now with f-strings, they’re actually faster performance.

Post back if you need more help. Good luck!!!

To add to Chris' remark about f-strings, I think they are about the best way to do formatting in modern Python. Search on treehouse for the Workshop called "What's new in Python 3.6", for a short description of how to use them.