F-strings In Python: Everything You Need To Know

preview_player
Показать описание

Python's F-strings are incredibly powerful. Knowing how to use them and taking advantage of their formatting options can significantly improve your ability to write logging messages and display information in an understandable manner. In this video, I'll take a deep dive into F-strings and demonstrate some of their uses.

🔖 Chapters:
0:00 Intro
1:07 What are F-strings?
1:26 Formatting numbers
5:27 Padding and alignment
8:58 (Data)classes, Str and Repr
13:01 Date and time formatting
16:11 Dealing with special characters
19:01 Printing variables for debugging purposes
20:09 Multiline strings & comments
21:20 F-strings performance
22:34 Advantages of Python f-strings

#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

Great video! Maybe I missed it, but did Arjan mention using using braces inside braces for variables like number of spaces to use in alignments?

>>> list_of_words = ['a', 'bc', 'def']
2 just = len(max(list_of_words))
3 for word in list_of_words:
4 print(f"This word: {word:>{just}}")
This word: a
This word: bc
This word: def

OMGnotThatGuy
Автор

Oh, yes. My favorite dessert for fridays lunches: Arjan videos.

carecavoador
Автор

I watch Arjan in the morning with my coffee. He just has such a pleasant, relaxing voice. I don't often write anything large in Python anymore, but I use Python extensively in security tasks... these videos are always informative and contain little bits of info that are often overlooked. Arjan, I would watch a video on Pendulum in a heartbeat!

meliodas
Автор

There is only one case where Template strings are preferable over F-strings. While you are working with databases and sql queries f-strings could be easily injectable by sql injection attacks. Template strings can’t be injected.

lukajeliciclux
Автор

Thank you for the video. Seems worth mentioning: if your class implements __format__, then f"{var:str}" equals format(var "str"). In particular, the documentation for string.format and datetime.datetime.format may help explain many of the examples presented here.

jeancerrien
Автор

Really love your videos! Interesting, fast, and useful. Plus the perfect length. Thanks so much for this.

pamdemonia
Автор

Thanks for explaining this so well. Cleared the 'fstring brain fog' I was in!

Lou-mwuv
Автор

format() is useful in formatting string variables, which can't be done with f-strings. In some cases, I store frequently-used strings in my code as constant to prevent hard-coding. I use format() to format variables into the string constant.

for example:
STRING = "Hello, {}"
print(STRING.format("Sara"))

output:
Hello, Sara

הודגמרסני-תל
Автор

I wasn't sure how you were gonna talk about f-strings for 20 minutes, but you made me realize how much I don't know, and every minute count. Amazing video as always Arjan

equu
Автор

Thanks for this video! It's amazing to see a experienced programmer explaining in deph a concept that will considered for beginners.

Warclimb
Автор

Thank you Arjan. I have been using F-string for sometime now but didn't know how robust I could use them like you did in this video. In my most recent uploaded video I created a class for polynomial mathematical functions and F-string helped a lot in the development of the __str__ method.

dalicodes
Автор

As always I love your videos, the only thing I’d prefer is if you could leave the example output on screen for one second or so longer, eg: datetime formatting section

agb
Автор

Thank you for taking the time to make this video. I have been looking for a video like this for a while.

hdb-
Автор

If I remember it correctly str_format will be outperforming any other formatting once there are a lot of variables called. I believe in Python 3.9 that would have been a string printing out 13 different variables.
For me this would be a very artificial case, but also the one aspect which speaks in favour of str_format.

Also all the lovely features of f-strings were covered, which delights me. Good job on that :)

PhyFlame
Автор

Excellent. There are lot of doubts cleared on "f-string" formatting.

rahulkmail
Автор

I think you read my mind. This is exactly what I was looking for! Thank you!

germanicus
Автор

Thank you very much for the content and valuable lesson. I'd really like to learn more about pendulum, will you create a class about it as well?

renatopiacente
Автор

Thank you, Arjan. I 've been using f strings since they came out and i still learned a thing or two.

MedievalChips
Автор

Particularly liked the multi-line use. 👍👏

davidjnevin
Автор

I'm a big fan of Arjan's videos because he is a strong proponent of simple and easy-to-read code. I wish we had a couple of Arjan clones at my company 😀

twelvethis