5 Useful F-String Tricks In Python

preview_player
Показать описание
Here are my top 5 most useful f-string formatting tricks that I use everyday in Python.

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

the "=" trick in the f-string is fire.

chyldstudios
Автор

About the fourth trick: the f in ".2f" tells it to format as float. You can also do for example ".2e"" which will format the number in scientific format.

Zenivo
Автор

I've been using python for about 10 years, and f strings extensively, but never knew that last tip! Game changer!

andymitchell
Автор

Ever since I learned f-strings...I love them.

sunwukong
Автор

This is the first time I have seen someone specify datatype for variables in python, and I honestly loved it.
Great tips btw.

_Loki__Odinson_
Автор

Very nice. Another useful is formatting float as percent: f"{foo:.2%}".

BohumirZamecnik
Автор

Didn't know about the date/time and equals formatting. Looks like the first one forwards to strftime. Makes things so much more concise and readable.

TheJaguar
Автор

Great video! I missed the bonus tip where you explain that format string calls __format__ on the object being formatted, so you can do your own formatting, like this:

class MyData:
def __init__(self, a: int, b: int, c: int):
self.a = a
self.b = b
self.c = c

def __format__(self, spec):
if spec[0] not in self.__dict__:
sep = spec[0]
l = list(spec[1:])
else:
sep = ', '
l = list(spec)

return for key in l)


my_var = MyData(a=1, b=2, c=3)
assert f"{my_var:cba}" == "3, 2, 1"
assert f"{my_var:-abc}" == "1-2-3"

rolandsz
Автор

I never use anything other than f-strings when printing and these tips are great. Going to use them!

enriqueDFTL
Автор

Thank you, this went STRAIGHT into my current project. Commas in numbers was one of the next things I was going to look up.

im_a_surfingdoggo
Автор

Great video! Didn't know about the datetime and debug print ones. Definitely going to use them in the future, though.

TheMcSebi
Автор

i love f strings
also this is like the 3rd time i come across the = specifier but i keep forgetting its existence and type in the whole thing

quekki
Автор

This kind of tips are awesone. We need more 👍

krzysiekkrzysiek
Автор

"Simple y bello como un anillo", como diría Neruda; pero además, muy funcional. ¡Muchas gracias!

SergioYT
Автор

To use scientific notation with integers, you can either do int(2e9) or 2*10**9.

mad_vegan
Автор

For those who didn't know, the last one is called self-documenting expression and was released in Python 3.8

UndyingEDM
Автор

This series of vids are the best on youtube - keep it going please, best sub ever.

KinkyJalepeno
Автор

First is fire
Second is fire(unless filling, I know this)
Third is like in C#, but without f string
Fourth I know, and also like in C#(unless, .3f, this is fire)
Fifth is fire, this is why I live python)
1year+ of python)
I use it in cryptography, and numerical methods)

SobTim-euxu
Автор

I like that print(f’{a + b = }’) one at the end. I can think of a few times when I’d use that.

daveys
Автор

Only the last one is specific to f-strings though, right? The rest are all just general string formatting techniques. Good tips nonetheless.

nuynobi