Every F-String Trick In Python Explained

preview_player
Показать описание
In today's video we're going to be exploring every major f-string feature in Python. It's good to know about these if you love f-strings!

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Learning Python made simple00:05 Intro
00:31 How fstrings work
02:42 Quick debugging
04:13 Rounding
05:42 Big numbers
08:10 Datetime objects
09:34 French strings
11:48 Nested strings
13:02 Alignment
16:20 Custom format specifiers
19:23 Conclusion
Рекомендации по теме
Комментарии
Автор

you can also change bases in F strings:
>>> f'{4:0b}'
'100'
>>>

U.k
Автор

15:05 Left alignment is the default for strings, but right alignment is the default for numbers.

mrab
Автор

Another nice formatted for numbers
num = 123.4566
print(f‘{num}:.5g’)
is 123.46
nice way to print a certain significant figures

lordstevenson
Автор

Formatting datetime with a variable format like you did with the alignment also is very handy and works combined with the debug specifier too

>>> df="%H:%M"
>>> print(f"{now:{df}}")
19:30

>>> print(f"{now=:{df}}")
now=19:30

SaschaMarkus
Автор

When specifying the number of decimal places, it doesn't just truncate, it also zero pads if needed. I.e. f'{1.2:.3f}' results in 1.200 as the result. This can be very useful in getting a column of numbers to align.

johnbennett
Автор

15:34 was completely new to me!

I've been coding Python for years, and this is so helpful!

TheFerdi
Автор

13:00
I happen to nest f-strings quite a lot as I spend a lot of time converting files to some obscure format with optional fields

This allows me to do things like

with io.StringIO() as sio:
sio.write("something {f'hi {x}! 'if x is not none else ''}")

Thats a small example but i really think that nested f-strings help readability in those cases

marceauh
Автор

Dynamic spacing saved my life, thank you very much. At 15:30 for anyone wondering.

TsankharXXI
Автор

excellent, now i need to do some edits to one of my programs

sovereignlivingsoul
Автор

12:46 I've already needed to nest f-strings together. I don't really remind when I did it, but I have a good example from the video itself:

Let's say you want to format the thousands separator to be a point instead of commas or underscore. You could do f'{f'{your_number_here:_}'.replace('_', '.')}'.

elfoyoutube
Автор

Raw strings are used extensively for regex patterns, so French strings (love that by the way!) are great if you need variable regex patterns

ROldford
Автор

Nested F-strings are useful if you want to nest an if or generator expression in an f-string.
In theory I think you could probably find some use of the walrus operator in there too but none come to mind

mightyhelper
Автор

12:44

print(f"The project {"was a success" if success else f"didn't run properly because {"there was an error: {exception}" if exception else "something unexpected happened"}"}")

rsvdark_main
Автор

I recently used f-strings to format a number to a certain length and start with zeros. It was really useful to create IDs for elements of a table
My code went like this
row = 4
length = 3
formatted_row = f'row{row:0{length}}'

Dubbidu
Автор

16:01 you know pycharm has an block edit mode? Click middle mouse button and drag over all 20, then paste

usthzng
Автор

this is awesome, I finally got bumped up to 3.12 (security restrictions)..TODAY, and I am behind on f-strings. I'm concerned they're not pythonic tho, we'll see. I still haven't learned format strings, which I first saw in 1987.

DrDeuteron
Автор

Sir I'm big fan
Plz 1 tutorial pymongo plzz

Specially only one document insert multiple time data plzz tell me


Mere khne ka matlab hai pymongo m collection m ek hi document ko use krke new data br br usi m kaise store kare

TRADINGPULSE
Автор

The fr string is pretty useful to write regular expressions

SantiagoRojo
Автор

i can see nested f string together with inline array construction/conversion
like f" something something {[f"{i.name}:{i.place}" for i in places]} something something "

schwingedeshaehers
Автор

❤❤
Please, I would like to know the name of the theme you use in PyCharm😊. Thank you

Ismail_Hassan-