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 trialGabriel Song
3,839 PointsWhat is "print(f" that is being used by the instructor?
The instructor wrote print(f"some text") instead of print("some text"). I'm confused about her decision.
3 Answers
Chris Freeman
Treehouse Moderator 68,428 PointsIn Python 3.6+, an f-string is newer way to format a string where variables and statements can be used directly with in the string.
In the two example you’ve included, there aren’t any references so the two are equivalent.
Post back if you need more help. Good luck!!!
Carl Crowther
5,005 PointsAshley went over f-string's in a previous video as a new way to format strings, as Chris said.
In her example:
for key, value in kwargs.items():
print(f'{key}: {value}')
over
for key, value in kwargs.items():
print('{}: {}'.format(key, value))
both amount to the same, certainly the newer way is easier to read :)
jaysonmena
12,539 PointsUsing this older form of string formatting, the list is being outputted in opposite order from Ashley's output. Why?
Ashley's output:
name: Ashley
job: Instructor
topic: Python
When I use:
for key, value in kwargs.items():
print('{}: {}'.format(key, value))
I get:
topic: Python
job: Instructor
name: Ashley
Chris Freeman
Treehouse Moderator 68,428 PointsThe output order of the key / value pairs depends on the hash values of the keys. This ordering may change depending on the order the items were added to the dictionary or the hashing used in a specific version of Python.
What should not change is the ordering the same version of Python.
rubiks69
1,647 PointsA good video explaining f strings is https://youtu.be/nghuHvKLhJA