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 JulianV

JulianV on March 20, 2019


Avatar image for Bomes

Bomes on March 23, 2019

Great tutorial! Thanks

Avatar image for Peter D K

Peter D K on March 26, 2019

f-Strings are somewhat smart! Thanks

Avatar image for davebmw325

davebmw325 on April 5, 2019

ExcellentTutorial. ShameThisCommentsBoxSpacebarDoesntWorkItJustSeemsToActAsThePauseButton.

Avatar image for Chris Bailey

Chris Bailey RP Team on April 9, 2019

Thanks davebmw325, I mentioned the issue of the spacebar not working to Dan. Were you using a mobile device (phone or tablet) at the time? I experienced the same thing when I tried to reply from my iPad.

Avatar image for Dan Bader

Dan Bader RP Team on April 17, 2019

@davebmw325: Thanks for the heads up :) This was an issue with the comments field that affected Safari on iOS. I’m rolling out a fix now, so this should be resolved shortly.

Avatar image for rklyba

rklyba on June 24, 2019

Thank you Chris for this course.

Avatar image for Mallesham Yamulla

Mallesham Yamulla on Sept. 2, 2019

Many thanks for taking us though the concepts from Old String Formating to New F string formatting.. Highly recommended to the python folks…

Avatar image for teodorwisniewski

teodorwisniewski on Oct. 20, 2019

Thanks you Chris, it is very useful and allowed me to understand some messy syntaxes used by some developers around strings.

Avatar image for Pygator

Pygator on Dec. 24, 2019

Always informative and right to the point Chris!

Avatar image for km

km on Jan. 6, 2020

THanks a lot.

Avatar image for Phil M

Phil M on Feb. 14, 2020

Excellent - thank you!

Avatar image for mikesult

mikesult on Feb. 21, 2020

Thanks for reviewing the old style of formatting in addition to the f-string. I especially liked your discussion about the use of different type of quotes and triple quote usage.

Avatar image for Zarata

Zarata on April 30, 2020

Yes, thanks. Two questions (or maybe 3): a) Are there other “special” flags that trigger different behaviors such as “!r” to use the representation rather than string? b) Are there flags and format codes as in “old style” to get output presentations precisely the way one would wish (decimals to certain number of places, time formats, etc.), c) Is there another course or video showing such depths?

Avatar image for Chris Bailey

Chris Bailey RP Team on April 30, 2020

Hi @Zarata,

a) Yes. There are a couple of others, !s Convert with str(), !a Convert with ascii()

b) Yes. The old style flags work here also.

>>> temperature = 78.8765
>>> print(f'Today it was {temperature:.2f}') # Float to 2 decimal places
Today it was 78.87

c) Not a course as of yet, but a detailed article here on Real Python. A Guide to the Newer Python String Format Techniques This covers the old style .format() methods, which f-strings support, and the conversions. I hope this helps!

Avatar image for alvesmig

alvesmig on June 28, 2020

Thank you

Avatar image for DoubleA

DoubleA on Feb. 12, 2021

Hi Chris! Thanks for the course. Am I right saying that f-strings feature all the same functionality as the previous % and <str>.fortmat() formatting methods? Or, is there any functionality which f-strings do not provide? Cheers.

Avatar image for Chris Bailey

Chris Bailey RP Team on Feb. 12, 2021

Hi DoubleA, I’m so glad you enjoyed the course. Yes, the formatting methods are mostly the same. This course shows of more of them in action. The <format_spec> Component

And this article has a specific section talking about the limitations: A Guide to the Newer Python String Format Techniques: f-String Expression Limitations

I hope this helps.

Avatar image for DoubleA

DoubleA on Feb. 13, 2021

Thank you so much for the references. Going to check those out! Keep on delivering the great stuff! 🔥🔥🔥

Avatar image for useeme2ndtime

useeme2ndtime on April 21, 2021

This is absolutely amazing!

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

Become a Member to join the conversation.