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

.format outside of strings

Hello, So I am almost done with Python Collections and a thought occurred to me. I had just done a four step code challenge all about dictionaries and returning a specific key, with a specific list, at a specific time (when the teacher has the most classes that's who gets returned). And it took 4 functions and I don't even know how many lines of code, but now I am learning about tuples and I realize you can just make a string with a few {} and .format and get the same results as the dictionary code challenge. So why didn't the code challenge just ask us to use {} to return the specific key with a specific list? Can you even use {} and .format outside of strings? For example can I do

return {}: {}.format(dictionary[key], dictionary.value())

or does that .format only work with strings? If so why? (I realize that the single line of code I gave wouldn't have completed the whole code challenge alone, but it would have cut off a few lines of code and save some time)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

.format() is a str method meaning it can only be used on str objects. The format method scans a string looking for fields designated by {} and replacing the fields with its arguments supplied.

Okay thank you!