F-Strings Have A Lot of Format Modifiers You Don't Know

  Рет қаралды 11,478

NeuralNine

NeuralNine

Күн бұрын

Today we learn about all the different F-String format modifiers in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: www.neuralnine.com/books/
💻 The Algorithm Bible Book: www.neuralnine.com/books/
👕 Programming Merch: www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine.com/
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/NeuralNine
🎙 Discord: / discord

Пікірлер: 42
@phsopher
@phsopher 8 ай бұрын
Another thing worth mentining is that you can define your own custom formatting rules for your classes by implementing the ___format___ method.
@dandyDev69
@dandyDev69 8 ай бұрын
thats fkn sick i never knew that till now
@capsey_
@capsey_ 8 ай бұрын
Note: KZfaq has a feature that when you surround text with underscores it becomes italic, so the name of the method in OP's comment appears to have only one underscore on each side instead of double underscores
@phsopher
@phsopher 8 ай бұрын
@@capsey_ Thanks, didn't know that. Edited to have three underscores now :)
@rikmoran3963
@rikmoran3963 8 ай бұрын
It's import to realise that you don't just use f-strings for printing, sometimes they are great for converting data from one format to another. If you use the bin() function to convert a number (n) into a binary string, it adds a '0b' prefix, which you may then need to remove using slicing. You could just use f'n:b' to do the same thing, or for example, if you use f'n:032b' you get a 32-bit binary string with all the leading zeroes included! Much simpler than writing some code to do the same thing.
@BLRMalcolm
@BLRMalcolm 7 ай бұрын
Very useful. Those IC.enanle and disable were amazing for verbose functions and argparse. Tysm
@paulthomas1052
@paulthomas1052 8 ай бұрын
Great tutorial as usual - very useful tips for formatting.
@josemanuelfernandez4999
@josemanuelfernandez4999 8 ай бұрын
Awesome!!! Very useful. Thanx a lot
@HypnosisBear
@HypnosisBear 8 ай бұрын
Nice! 2:16
@pitaeata8493
@pitaeata8493 8 ай бұрын
great to know this stuff. thank you.
@Bebebaby1988
@Bebebaby1988 8 ай бұрын
Great! Are you planning to make a second edition of the Python Bible book? It's been 2 years since the first edition published, can't wait for continuation!
@pjb8892
@pjb8892 8 ай бұрын
Everytime I have a question about python I end up at your channel. Your videos are very informative and great to learn from! PS: How is the song called in your intro?
@phantom7333
@phantom7333 8 ай бұрын
Hello there! In 11:10, I'd like to do something like this: day = 7 month = 6 year = 2023 MAX_DAYS = 31 MAX_MONTHS = 12 print(f"{day:len(MAX_DAYS)}.{month:len(MAX_MONTHS)}.{year}") Also, if we wanted to have the binary in 11:40 like 0100_1001 etc and not 100_1001 etc. Like add the last 0 when cutting it in bits of 4 or 8, how could we do that?
@clementihammock7572
@clementihammock7572 8 ай бұрын
thank you, py mentor.
@elarrayhesohit4479
@elarrayhesohit4479 8 ай бұрын
what key stroke did he use at 10:13 to delete every in the format string?
@daveys
@daveys 4 ай бұрын
Very useful :-)
@khandoor7228
@khandoor7228 8 ай бұрын
when you did dollar values how did it know to separate after 3 digits? Is that default behavior?
@BokoMoko65
@BokoMoko65 8 ай бұрын
use grouping=True on the locale. Currency(value, grouping=True)
@khandoor7228
@khandoor7228 8 ай бұрын
@@BokoMoko65 ty
@XxLeonardoPiresxX
@XxLeonardoPiresxX 7 ай бұрын
_amazing_
@akunbodong8971
@akunbodong8971 8 ай бұрын
Hey, when will you talk about the real company Cannafarm Ltd that brings profit?
@Heavy_Lvy
@Heavy_Lvy 8 ай бұрын
Why dont you use python 3.11?
@prashantm9856
@prashantm9856 8 ай бұрын
Do Little work on intro❤❤❤
@unotoli
@unotoli 6 ай бұрын
Not a function, not a class (same 'str'), just a dirty hacks that looks like 'salty syntax'. Maybe all show go write simple, readable code instead?
@MariusDiMaio
@MariusDiMaio 8 ай бұрын
the first thing, you are good when explain python. And the second thing, i like, because you use pycharm and not VS Code, of microsoft. Never, i will use ide of microsoft in my linux
@rodrigog.torres9104
@rodrigog.torres9104 8 ай бұрын
To the moon with Cannafarm Ltd!
@johanildpg5414
@johanildpg5414 8 ай бұрын
First!
@ButchCassidyAndSundanceKid
@ButchCassidyAndSundanceKid 8 ай бұрын
so what ?
@essa88371
@essa88371 8 ай бұрын
second!
@v11r
@v11r 8 ай бұрын
Hi, Why this doesn't work? import locale import datetime current = datetime.datetime.now() locale.setlocale(locale.LC_NUMERIC, 'fr_CA.utf-8') print(f'current = {current:n}') Displays: current = n NO ERROR Python 3.11.4
@phsopher
@phsopher 8 ай бұрын
When asking for help it's generally a good idea to include the error that you're getting. Works fine for me
@v11r
@v11r 8 ай бұрын
@@phsopher I updated the code, no error. What you have displayed?
@phsopher
@phsopher 8 ай бұрын
@@v11r 'n' is used for formatting numbers not dates (thousand separators, decimal point). I assumed current was a number since you didn't have it defined in the original comment.
@v11r
@v11r 8 ай бұрын
Found solution: import datetime from babel.dates import format_date, format_datetime, format_time print(format_datetime(datetime.datetime.now(), locale='fr_CA.utf-8')) Displays: 19 sept. 2023, 15 h 34 min 30 s
@phsopher
@phsopher 8 ай бұрын
@@v11r Great! That's another library that implements its own formatting function though so it's not something built into F-strings. If you want the functionality you had in your original comment you can extend datetime by implementing your own __format__ method which uses babel and returns the format you want.
@thomasgoodwin2648
@thomasgoodwin2648 8 ай бұрын
When I think back on all the time I wasted trying to do this stuff manually or just ignoring the ugly non formatting... 🤔😲🤯 This kinda highlights one of my biggest complaints in the whole world of programming. The sheer amount of minutia and details to memorize is overwhelming at times. It gets worse the more languages you accumulate. Everyone seems to have their own nomenclature and semantics. "MATCH case not SWITCH case which was something we were all used to BEFORE, thank you very MUCH! " Sometimes the function you want is right under your nose but you lack the nomenclature to know what your looking for. On the other hand even once you DO find that weirdly specific thing in the obscure library, often you wind up having to immerse yourself in Vogon poetry (Little 'Hitchhiker's Guide' reference there.. ) in order to use it. APIs are not always intuitively written, or have any reasonable docs (and usually tons more obscure minutia to memorize). THEN we get into the whole issue of licensing. Just a cluster#$^@. "Free for use.. On alternate Tuesdays IF you have a counter-signature from the Burgermeister of Sonoma." Sometimes just our limited capacities to completely grok the capabilities of a full language suite is the smallest bottleneck to creativity. Anyways, enough soapbox. I prostrate myself before thee Shifu! "May the luck of the 7 pillars of Gulu smile upon you at all times." 🙇‍♂👍
@NothingHere-qk1im
@NothingHere-qk1im 6 ай бұрын
Locale that you used is not working for me. locale.setlocale(locale.LC_NUMERIC, locale: 'pl_PL.utf-8') ^ SyntaxError: invalid syntax
Data Classes in Python Are The New Standard
20:34
NeuralNine
Рет қаралды 65 М.
Garbage Collection in Python: Speed Up Your Code
16:41
NeuralNine
Рет қаралды 13 М.
1 класс vs 11 класс (неаккуратность)
01:00
БЕРТ
Рет қаралды 2,3 МЛН
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 2,1 МЛН
OMG 😨 Era o tênis dela 🤬
00:19
Polar em português
Рет қаралды 9 МЛН
Python string format 💬
12:47
Bro Code
Рет қаралды 28 М.
F-strings In Python: Everything You Need To Know
23:29
ArjanCodes
Рет қаралды 48 М.
Stuck in Nested Loops? Here's How You Leave Them!
11:33
NeuralNine
Рет қаралды 7 М.
Unpacking Operators in Python: What are * and **?
12:35
NeuralNine
Рет қаралды 9 М.
Python string slicing ✂️
11:37
Bro Code
Рет қаралды 56 М.
How To Use: "@dataclass" In Python (Tutorial 2023)
15:01
Indently
Рет қаралды 24 М.
What Does "yield from" Do in Python?
5:37
NeuralNine
Рет қаралды 5 М.
JSON Schema Validation in Python: Bring Structure Into JSON
13:45
Дени против умной колонки😁
0:40
Deni & Mani
Рет қаралды 8 МЛН
Apple watch hidden camera
0:34
_vector_
Рет қаралды 48 МЛН
Carregando telefone com carregador cortado
1:01
Andcarli
Рет қаралды 1,7 МЛН
Power up all cell phones.
0:17
JL FUNNY SHORTS
Рет қаралды 48 МЛН
Я Создал Новый Айфон!
0:59
FLV
Рет қаралды 4,6 МЛН