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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Luke Maschoff](https://secure.gravatar.com/avatar/8c0114cbda39efa526cb1148b9b12944?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Luke Maschoff
820 PointsIn Python, what is the purpose of .format()?
Confused on what .format() does in Python.
2 Answers
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe "format" method looks through the string it is applied to for placeholder tokens (usually "{}"), and replaces each of them with one of the arguments passed to it. It's a good way to add data from variables into messages.
For more details, see the Custom String Formatting section of the Python reference manual.
![Øyvind Andreassen](https://secure.gravatar.com/avatar/324522a518e3d366eb1e0b447a06432f?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Øyvind Andreassen
16,839 PointsBoth solution will work, I haven't heard that keyword arguments are deprecated as they give very clear instructions on what should go where. Tested in Python 3.7.0.
With the f-strings in Python you can also do:
def favorite_food(name, food):
return f"Hi, I'm {name} and I love to eat {food}!"
This article goes through pros and cons with all approaches.
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsTake a look at the manual page I linked in the answer. It contains this:
Changed in version 3.7: A format string argument is now positional-only.
Am I misunderstanding that?
![Mark Nembhard](https://secure.gravatar.com/avatar/ff16d556c674b5ad4a39d15d94b72d2a?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Mark Nembhard
1,387 PointsHi . the chat about the deprecated format is interesting. If you have quite a few place holders then having the names of the string variables that you want to place in them makes sense and less error prone. So instead of the .format(x,y,z) variables that have to go in sequential order you can just list the variable names and they will jump into the place-holders where they are mentioned. Please correct me if i am wrong as i am new to this
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe keyword arguments can be handy, and they do still work for now, but the manual says they are no longer "official". As to why they are moving away from them, I have no idea!
Øyvind Andreassen
16,839 PointsØyvind Andreassen
16,839 PointsTo elaborate on Steven's answer.
If you want to have a string formatted a certain way each time you could do something like this:
So each time you call
favorite_food
you can pass in a name and a food and get the string you expect.Steven Parker
230,688 PointsSteven Parker
230,688 PointsIf I understand what I just read in the manual link given above, keyword argments are now deprecated for "format". So refactoring that example for the current version might look like this: