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 All Together Now Gather Information

Derek Lefler
Derek Lefler
2,608 Points

Is 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi Derek, the reason it's used .format() instead of f'{}' to be compatible with python 2.x as well not just 3.5 +. The f'{}' 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/.

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

In 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
Chris Freeman
Treehouse Moderator 68,457 Points

Oszkár Fehér, can you give an example of where you would used format() over an f-string?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Oszká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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi 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
Chris Freeman
Treehouse Moderator 68,457 Points

Oszkár Fehér, good examples. It’s always about keeping it readable over blind adherence to the latest methods.

Derek Lefler
Derek Lefler
2,608 Points

Chris Freeman

It’s always about keeping it readable over blind adherence to the latest methods.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Thanks for the attribution. I feel Oszkár Fehér deserves the Best Answer.