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 Introducing Dictionaries Iterating and Packing with Dictionaries Packing with Dictionaries

What 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
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

In 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!!!

Ashley 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 :)

Using 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
Chris Freeman
Treehouse Moderator 68,423 Points

The 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.

A good video explaining f strings is https://youtu.be/nghuHvKLhJA