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 trialDerek Lefler
2,608 PointsIs there a reason we're using the .format syntax instead of f-strings?
In this video the example is: print("There are {} tickets remaining: ".format(tickets_remaining))
But with an f-string it's shorter and more readable in my opinion": print(f"There are {tickets_remaining} tickets remaining: ")
Oszkár Fehér
Treehouse Project ReviewerIn some situation, it's still better to use .format()
even in python 3.5+, especially if you need to feed in multiple arguments, practice will show the difference between the two usages.
Chris Freeman
Treehouse Moderator 68,441 PointsOszkár Fehér, can you give an example of where you would used format()
over an f-string?
Derek Lefler
2,608 PointsThanks!
Chris Freeman
Treehouse Moderator 68,441 PointsOszkár Fehér, good examples. It’s always about keeping it readable over blind adherence to the latest methods.
3 Answers
Oszkár Fehér
Treehouse Project ReviewerHi Chris. I didn't dig too deep in my codes but I will if you require, these are what I use most of the time. One of the usages what I found better when we pass in a dictionary, ex.:
dict_ = { 'first_obj': 'value', 'second_obj': 'value}
print("{first_obj} followed by the {second_obj}".format(dict_))
Or another what I found more clean and clear in django or flask, using the form
"Update {}".format(form.cleaned_data['field'])
over
f"Update {form.cleaned_data['field']}"
All above mentioned examples it's just my opinion, probably I should correct or delete the last comment.
Chris Freeman
Treehouse Moderator 68,441 Pointsmoved comment to answer.
Chris Freeman
Treehouse Moderator 68,441 PointsOszkár Fehér, good examples. It’s always about keeping it readable over blind adherence to the latest methods.
Oszkár Fehér
Treehouse Project ReviewerThank you Chris
Derek Lefler
2,608 PointsIt’s always about keeping it readable over blind adherence to the latest methods.
Chris Freeman
Treehouse Moderator 68,441 PointsThanks for the attribution. I feel Oszkár Fehér deserves the Best Answer.
Oszkár Fehér
Treehouse Project ReviewerOszkár Fehér
Treehouse Project ReviewerHi Derek, the reason it's used
.format()
instead off'{}'
to be compatible with python 2.x as well not just 3.5 +. Thef'{}'
it's in python 3.5+ and it will not work in python 2.7 for example, but.format()
it's still implemented in both versions. Check out this link https://realpython.com/python-f-strings/.