Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

F-Strings Conclusion—Go Forth and Format!

This lesson concludes Python 3’s f-Strings: An Improved String Formatting Syntax. You’ll watch a review of everything you have learned in this course.

Avatar image for Jon David

Jon David on Oct. 7, 2021

Thanks for this. I will be using f-strings from here on.

Feel free to laugh, my way of doing things before was to do like this:

print(‘This is the start ‘+str(variable_name)+’ and this is the end.’)

If you ask me, this is more like f-strings in terms of how it reads than either %-formatting or the .format method. Maybe that is why I was intuitively drawn to it!

Avatar image for aniketbarphe

aniketbarphe on Nov. 7, 2021

Thank You!

Avatar image for amack604

amack604 on May 22, 2022

Thanks for this tutorial.

I would like to display a float number with a number of decimals specified as a function argument.

”% style” works:

def format(float_number, decimal_places):
    return '%.*f' % (decimal_places, float_number) 
    # format(pi, 2) returns 3.14; format(pi, 3) returns 3.142 etc.

But all attempts to accomplish this with f-strings have so far failed. For example, I tried this:

def format(float_number, decimal_places):
    specifier = f':.{decimal_places}f'
    return f'{float_number}{specifier}'
    # format(pi,2) returns 3.141592653589793:.2f

Everything else I’ve tried either just returns the number with format specifier appended, as above, or results in “ValueError: Invalid format specifier”.

Is it possible to replicate this functionality using an f-string?

Avatar image for Martin Breuss

Martin Breuss RP Team on May 22, 2022

Hi @amack604, yes you can do this with an f-string:

from math import pi

def format(float_number, decimal_places):
    return f'{float_number:.{decimal_places + 1}}'

format(pi, 2)  # 3.14
format(pi, 3)  # 3.142

There’s a LOT you can do with that string formatting mini-language, which I think you already know about. :)

For applying some of the specifiers in f-strings as variables, seems like you need to use another set of squiggly braces in order to input the value.

Avatar image for amack604

amack604 on May 22, 2022

Thanks very much, Martin. Thought I’d tried the same nesting you used, but obviously not! :)

Interestingly, this code yields the same result as yours:

def format(float_number, decimal_places):
    return f'{float_number:.{decimal_places}f}'
Avatar image for Martin Breuss

Martin Breuss RP Team on May 22, 2022

Oh! Well that’s 100 times more beautiful than mine @amack604 😃

I did wonder about why I’d have to add 1 but was too lazy to play around with it more 😂…

Nice work! 🙌

Avatar image for Francisco Carlos

Francisco Carlos on June 3, 2022

Liked it! Gives tips and study guidance for those starting out. we learned the evolution of f’ in python writing, and how to make it as pythonic as possible and current.

Avatar image for Erik Dyrelius

Erik Dyrelius on Aug. 25, 2023

If you would like to add a small disadvantage of f-strings, it is that they are immediate. If you wanted to send, say a string template and a dictionary into a function that would “apply” the f-string to the dictionary, it wont work. E.g. using f-strings for different ways of formatting a Date. There a .format would probably work better.

Avatar image for Martin Breuss

Martin Breuss RP Team on Aug. 29, 2023

@Erik Dyrelius yes that’s a good point! They’re not helpful for templating. We’re currently updating the tutorial that this course is based and are also including that aspect. :)

Avatar image for Fahim Mahmud

Fahim Mahmud on Dec. 15, 2024

A good intro to f-string. I’ll be using them from now on. I previously looked at the f-string dubiously and thought what they were. Now I know! Yayy!

Become a Member to join the conversation.