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

What is the purpose of (f) outside a string within the print function?

Hi,

I found these lines of code in the community questions in reference to the last program in the python basics course, they replace the system error message with a nicer one, courtesy of Steven Parker.

if"base 10" in str(err): 
            print("oh no! that's not a valid entry!you must enter a number")
        else:
            print(f"Oh no, we ran into an issue. {err}. Please try again")

Could someone please elaborate and explain in further detail the first and especially the last line of the code here, I would love to know more of what the (f) does in outside of the string and how does the if statement work in the first line (if "base 10" in str(err):)?

3 Answers

Steven Parker
Steven Parker
231,248 Points

The first line is testing if the words "base 10" are part of the string that the "err" object will print out. That's a good test since it's one of the most awkward parts of the standard message. The "err" was received in the "except" by using the "as" operator.

The other part is an "f-string". It is essentially a more compact syntax for what you might do using the "format" function.

For more details, see the section on Formatted string literals in the online documentation or the page on Literal String Interpolation in the PEP guide.

They are called f-strings. You can read more on them here: https://realpython.com/python-f-strings/

In short they let you print out variables in your print statement.

e.g.

name.py
name = "Yahya"

# will print out "hello Yahya"
print(f"hello {name}")

Also the if "base 10" in str(err). in is a keyword that will test if element x is found in sequence y. You can use it on strings, lists, tuples, dicts, etc.

Hi, thank you for your response!

isn't that the same as

print("hello{}".format(name))

? :)

Never mind, just finished the article! :)

Hi Yahya

This is called f-string which was introduced in Python 3.6. In my personal opinion, f-string is a much easier string formatting than .format.

Here is an example for you:

lang = "Python"

# using .format
print("I love {} programming".format(lang))

# using f-string
print(f"I love {lang} programming")

"""
OUTPUT:

I love Python programming
I love Python programming
"""

With f-string you can write the variable name right inside the curly brackets {} which makes the code clearer and easier to follow.

Hopefully this helps!

Awesome, thank you!

Care to explain how this line works?

if"base 10" in str(err):

is the if statement referring to the system's error message in the output or am I wrong?.

Does that mean that str(err) is universal for the system's error message or just because that str(X) X = the error message inside the raise that was referenced to in except as X ?