F-strings In Python: Everything You Need To Know

  Рет қаралды 48,700

ArjanCodes

ArjanCodes

Күн бұрын

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.
➡️ Pendulum package: pendulum.eustace.io
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
💻 ArjanCodes Blog: www.arjancodes.com/blog.
🎓 Courses:
The Software Designer Mindset: www.arjancodes.com/mindset
The Software Designer Mindset Team Packages: www.arjancodes.com/sas
The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
The 30-Day Design Challenge: www.arjancodes.com/30ddc
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
💬 Discord: discord.arjan.codes
🐦Twitter: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
📱Instagram: / arjancodes
👀 Code reviewers:
- Yoriz
- Ryan Laursen
- James Dooley
- Dale Hagglund
🎥 Video edited by Mark Bacskai: / bacskaimark
🔖 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
DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Пікірлер: 237
@ArjanCodes
@ArjanCodes 7 ай бұрын
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis.
@carecavoador
@carecavoador Жыл бұрын
Oh, yes. My favorite dessert for fridays lunches: Arjan videos.
@ArjanCodes
@ArjanCodes Жыл бұрын
Enjoy :)
@OMGnotThatGuy
@OMGnotThatGuy Жыл бұрын
For sure. Best Python videos on KZfaq!
@carecavoador
@carecavoador Жыл бұрын
@@OMGnotThatGuy I really enjoy how he puts valuable knowledge on his videos. Even simple things like f strings can be really rich subjects while being pleasing to watch. Today I learned a lot o things I didn't know, for example.
@lukajeliciclux3074
@lukajeliciclux3074 Жыл бұрын
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.
@OMGnotThatGuy
@OMGnotThatGuy Жыл бұрын
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
@guyindisguise
@guyindisguise Жыл бұрын
Thanks! He didn't mention that and I've been looking for to do that a while ago.
@ArjanCodes
@ArjanCodes Жыл бұрын
Great tip, thanks!
@Amstelchen
@Amstelchen Жыл бұрын
07:35 It's a circumflex. In German, it is sometimes called "Dach" or more collquially, "Dacherl" which means "little roof" ^^
@illiterate467
@illiterate467 Жыл бұрын
That's a new word for me. Very cool. Alternatively, the ^ symbol is also called a "caret", which is what I've always called it and I think is probably the most common term in English (US).
@ChongFrisbee
@ChongFrisbee Жыл бұрын
Circumflex is a diacritic, meaning it is used to describe situations where other characters are modified, like ê for example. The isolated character is called caret
@Tweakimp
@Tweakimp Жыл бұрын
One reason to use % templates is in logging functions: The logger can skip the variable evaluation if the logging is skipped. This would not work with fstrings. Also you can not put backslashes in fstrings. f"{' '*3}" raises a SyntaxError
@notead
@notead Жыл бұрын
You can totally put backslashes inside f-strings, just not within the brackets.
@nigh_anxiety
@nigh_anxiety Жыл бұрын
the backslash can't be within the braces of the f-string expression, but you can include a string which contained a backslash escaped character. `x = ' '; print(f"{x*3} ")` is completely fine.
@Tweakimp
@Tweakimp Жыл бұрын
@@nigh_anxiety Thanks :)
@meliodas4560
@meliodas4560 Жыл бұрын
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!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you, glad you liked the video!
@pamdemonia
@pamdemonia Жыл бұрын
Really love your videos! Interesting, fast, and useful. Plus the perfect length. Thanks so much for this.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful!
@user-hf5be1zu7s
@user-hf5be1zu7s Жыл бұрын
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
@K1assh
@K1assh Жыл бұрын
Weird, but thanks for the example. Could come in handy. It's backwards to how I always use format(), which is an interesting way to think about it.
@parswarr
@parswarr Жыл бұрын
Yep, same. For really big string templates I'll even use the jinja template generator on rare occasion.
@Skrattoune
@Skrattoune Жыл бұрын
That's great! I have been looking for such a complete and clear explanation for quite some time. That makes f strings so much more useful !
@jeancerrien3016
@jeancerrien3016 Жыл бұрын
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.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you Jean, glad you liked the video!
@raphajptube
@raphajptube Жыл бұрын
f-strings cannot be used for creating templates. The code bellow would throw a NameError as 'number' is not defined. However it works with .formating. template = "my number is {number}" for i in range(5): print(template.format(number=i)
@alexvv2053
@alexvv2053 Жыл бұрын
I had to look it up: the 'little roof' is called a caret in programming
@nathanbrown2640
@nathanbrown2640 Жыл бұрын
I was going say, yes, ^ tends to get called 'caret' in English, at least in my experience here in the UK. Sometimes we might say 'circumflex', referring more to when it is a diacritic, especially in French (which is where I think many English speakers tend to meet it first)
@clasdauskas
@clasdauskas Жыл бұрын
aka 'hat' - now I want to know what the actual Dutch is.
@PhyFlame
@PhyFlame Жыл бұрын
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 :)
@henrybarth
@henrybarth Жыл бұрын
Thank you for taking the time to make this video. I have been looking for a video like this for a while.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Henry, glad the content is helpful!
@leidem
@leidem Жыл бұрын
Thanks for the video! I've found one niche use case for the old `format` method, which was in dynamic configurations. If I want the user to be able to specify a string (such as a filename), they can specify it as "file-{}-23", say, and various dynamic metadata will then be inserted into the filename using the `format` method.
@agb2557
@agb2557 Жыл бұрын
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
@erikchubb9704
@erikchubb9704 Жыл бұрын
When I need to study the code or output, I just hit spacebar (which pauses the video). If i don't need to see it, then the current editing style keeps things moving fast. So my preference is to stick with the current editing style
@dalicodes
@dalicodes Жыл бұрын
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.
@hammerheadcorvette4
@hammerheadcorvette4 Жыл бұрын
My first encounter with f'strings, I needed to mimic real world address data for a project. So creating a a variable with random.choice() or random.random() all the way through was fun. A names list for the street names, people name, and city names, a random number for street address and building or house number, random choice of apt/suite number, random zipcode etc. All in one fstring.
@jurgenrusch4041
@jurgenrusch4041 Жыл бұрын
Hi Arjan, again a very nice and complete video. Great! I do have one question: I love f-strings but in loggers I seem only to be able to use % for lazy evaluation. Loggers do support {} bu that only seems to be possible in the specification of the log-message. So, question: can I use f-string style {} for lazy evaluation of log messages?
@alexbigkid
@alexbigkid Жыл бұрын
@Arjan: love the f-strings. As you mentioned they were introduced in v3.6. However the f-string debugging version is valid from v3.8. My tox test were failing with 3.7. That is how I discovered. The reason why developers don’t use the new formatting is not to break backwards compatibility.
@Warclimb64
@Warclimb64 Жыл бұрын
Thanks for this video! It's amazing to see a experienced programmer explaining in deph a concept that will considered for beginners.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much, glad you liked the video!
@alxcnwy
@alxcnwy Жыл бұрын
Very cool and useful tips, thanks Arjan!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Alex, glad the content is helpful!
@germanicus1475
@germanicus1475 Жыл бұрын
I think you read my mind. This is exactly what I was looking for! Thank you!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful! :)
@Lou-mw8uv
@Lou-mw8uv Жыл бұрын
Thanks for explaining this so well. Cleared the 'fstring brain fog' I was in!
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad it helped!
@FabioKasper
@FabioKasper Жыл бұрын
Great tips, Arjan. Thanks!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Fabio, happy you’re enjoying the content!
@jace3789
@jace3789 Жыл бұрын
Ive followed quite a lot of your content enjoy your enthusiasm and knowledgeable videos.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Jace, glad the content is helpful!
@stevegrimes5105
@stevegrimes5105 Жыл бұрын
Thanks for another great video. I've been watching your videos during my lunch break each day and they've really helped me with my "100 days of Python" course.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, Steve the content is helpful!
@DrGreenGiant
@DrGreenGiant Жыл бұрын
^ is called a "caret" or a "hat" if on top of a character, in English. Thanks for the great videos
@eliavrad2845
@eliavrad2845 Жыл бұрын
4:00 Number literals ignore underscores*, so it's common to use f"(44_000_000_000:,.2}" instead of counting zeros. * things like 1_23_4.56_7 are allowed (if ugly), but leading and trailing underscores are forbidden from both sides of the decimal point, so _100, 3.14_, 9_.8, and 2._6 all throw Syntax Error
@LordDoucheBags
@LordDoucheBags Жыл бұрын
Easier to just use 44e9 as a float, then convert to int as you need to 🤷‍♂️
@dann1kid
@dann1kid Жыл бұрын
Thanks for dataclasses man, very useful and only you spot it for me
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful!
@user-hf5be1zu7s
@user-hf5be1zu7s Жыл бұрын
thank you so much, saved me a lot of work!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful!
@equu497
@equu497 Жыл бұрын
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
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful!
@rahulkmail
@rahulkmail Жыл бұрын
Excellent. There are lot of doubts cleared on "f-string" formatting.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Rahul, glad the content is helpful!
@dslackw
@dslackw Жыл бұрын
Many Thanks, veryyyyyy useful 👍
@WillyJL
@WillyJL Жыл бұрын
20:30 you can also use the multiline strings with fstring formatting, same as usual (3 quotes at beginning and end, with f before): sentence = f"""some multiline text"""
@TheSdfasdfsaf
@TheSdfasdfsaf Жыл бұрын
Hi Arjan, been watching (and subbed) since you only had less than 5k subscribers. Congrats on hitting 100k! The little roof is called a caret in English.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Matthew.
@user-cg6tx4pd2e
@user-cg6tx4pd2e Жыл бұрын
My favorite Python channel in youtube, thanks Arjan :)
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Frank, happy you’re enjoying the content!
@kevincodes674
@kevincodes674 Жыл бұрын
Great video, really helpful!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Kevin, glad the content is helpful!
@vk3fbab
@vk3fbab Жыл бұрын
Great video. The only thing you missed that I use all of the time is the triple quote multi line string. Using that you can use single and double quotes in your string to access dicts. Glad you skipped the most newbie form of string generation the evil and slow + operator. I saw a cheat sheet for python and it showed that, I thought whomever wrote that cheat sheet mustn't have used python much. I use and peach the use of f strings for all the reasons you mentioned. So readable and obvious.
@virtualraider
@virtualraider Жыл бұрын
Ah, I didn't see your comment before I replied with the same thing 😅
@safa_jahan
@safa_jahan Жыл бұрын
Thanks! Learned a lot.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Alireza, glad the content is helpful!
@kdt85
@kdt85 Жыл бұрын
Love the debugging tip!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad you liked it!
@tir0__
@tir0__ Жыл бұрын
Another gem from a great teacher ! 👌🏻👌🏻👌🏻👌🏻👌🏻
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful!
@stevenwilson2292
@stevenwilson2292 Жыл бұрын
Yes, do a video on pendulum please.
@davidjnevin
@davidjnevin Жыл бұрын
Particularly liked the multi-line use. 👍👏
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much David.
@bls512
@bls512 Жыл бұрын
I really enjoy learning from your approaches with python programming.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Bo, glad the content is helpful!
@silkogelman
@silkogelman Жыл бұрын
That debugging feature with the equal sign at 19:31 is going to save me time, thank you! I did not know we could apply Datetime in f-strings. Even math and assignment expressions! Awesome stuff. Pendulum looks interesting, Arrow comes to mind too.
@muthvar1
@muthvar1 Жыл бұрын
Is this debugging feature available in 3.6.8? I hit syntax issues in 3.6.8 when I try this
@polimorphic13
@polimorphic13 Жыл бұрын
@@muthvar1 I found this in other comment: " f-string debugging version is valid from v3.8".
@PaulSmith-zs5je
@PaulSmith-zs5je Жыл бұрын
This is a 1337 f string video.. Hat or Caret we call your little roof.. Keep up the great content.. Also something not mentioned in video is if you want to use an integer for the number of decimal places you need to use nested f-string formatting to achieve this.
@twelvethis3979
@twelvethis3979 Жыл бұрын
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 😀
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad you liked the video and the content is helpful!
@rogifedi1
@rogifedi1 Жыл бұрын
Great stuff thank you Arjan
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much Ste, glad you liked it!
@jefralston
@jefralston Жыл бұрын
Thanks Arjan for making this video (and all your videos). You are terrific at teaching. Please make the video on Pendulum if you have an opportunity.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks for the suggestions, Jeffrey, I've put it on the list.
@MedievalChips
@MedievalChips Жыл бұрын
Thank you, Arjan. I 've been using f strings since they came out and i still learned a thing or two.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad you liked it!
@pepecopter
@pepecopter Жыл бұрын
Hey Arjan. Thanks for the content. I'd love a Better Time video if this fits in your schedule! Cheers and happy weekend.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful!
@Melindrea
@Melindrea Жыл бұрын
ooh, this'll allow me to tweak some of the printing options that I haven't touched from my supervisor's code (apart from moving them). he and I don't have the same programming backgrounds, so while it's a bit too strong to say that we "disagree" ... we often don't do things in the same way.
@Daviid_5
@Daviid_5 Жыл бұрын
I read that repr should always return a string that, when pasted on the python interpreter, allows you to create an exact copy of the object
@biermeester
@biermeester Жыл бұрын
The __repr__ method should also return a unique string for each object, even when they have the same value. That's why the memory address is included in the output of the default __repr__ method.
@nordgaren2358
@nordgaren2358 Жыл бұрын
I have been using interpolated strings in C# since I discovered them. They are nice in python, too! I wish there was interpolated strings in C or C++, though!
@matteolatinov6630
@matteolatinov6630 Жыл бұрын
Great video, as always. One possible drawback of f-strings: can't use the /n character for multiline print if I'm not mistaken.
@planetshootproduction1721
@planetshootproduction1721 Жыл бұрын
Quite informative👌
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks ✌️
@aldrickdev
@aldrickdev Жыл бұрын
Hey Arjan, Great video here, there were actually many things here that I didn't know about f-strings. I would like to ask, do you have any plans of making videos on the mypy package? I am trying to get into the habit of using types in my code and its working great especially when used on my own code. The issue I run into is when interfacing with external packages and sometimes their functions have wacky types on their parameters or failing to narrow the type of a variable so that mypy knows that after a specific check a variable is only of a certain type. I feel that these are things that I don't really see covered in other mypy videos and wanted to see if maybe you would.
@renatopiacente5329
@renatopiacente5329 Жыл бұрын
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?
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks for the suggestions, Renato, I've put it on the list.
@matthiashomann6381
@matthiashomann6381 Жыл бұрын
Hi Arjan, what do you think about the pylint warning W1203 "Use %s formatting in logging functions" raised if f-strings are used log messages? As far as I understand it's to avoid the formatting operation if the log message is usually not shown (e.g. debug messages at info-level) and come corner cases where f-string might cause an exception.
@eskuAdradit0
@eskuAdradit0 Жыл бұрын
also for log seggregation.
@pikkusiili2490
@pikkusiili2490 Жыл бұрын
Formatted strings are left-aligned by default so the extra spaces in f"{greet:6}" appear on the right (i.e., "Hi "). The ">" switches this to right-aligned (which is the default for numbers).
@paulrobinshaw3593
@paulrobinshaw3593 8 ай бұрын
Hello. Great video. Big help. ^ in english is a caret 😊
@mehdicherifi6289
@mehdicherifi6289 Жыл бұрын
Great tutorial
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Mehdi, happy you’re enjoying the content!
@uniqe6812
@uniqe6812 Жыл бұрын
As always, this video is great
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Unique, happy you’re enjoying the content!
@alexanderpoplawski577
@alexanderpoplawski577 Жыл бұрын
Is it possible to use dynamic formatting in F-strings? Like, when you have a user definable number of decimals to show. In percentage formatting I would create the format string like "%%.%df" % (no_digits)
@kosmonautofficial296
@kosmonautofficial296 Жыл бұрын
Thanks for the breakdown of performance that is interesting! What did you mean when you said this feature is called debugging? Is that the f string term? I don’t think of printing variables as debugging. I was looking into jinja templates for creating some text config files but not sure how it will go compared to doing it with python loops and f strings. I wonder if f strings could ever get more functionality.
@chriskathumbi2292
@chriskathumbi2292 Жыл бұрын
Finally, a new class. Mind doing a video on reflection in python and how it works?
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks for the suggestions, Chris, I've put it on the list.
@ramimashalfontenla1312
@ramimashalfontenla1312 Жыл бұрын
Really useful!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you Rami, glad you liked the video!
@virtualraider
@virtualraider Жыл бұрын
Great video as usual. Small nit, tho: using single quotes within f-strings isn't the only way to access quoted values like dict's. You can also use triple quotes. It's noisier, but you can 😉 `print("""Like so: { dictionary["key"] }""")`
@ArjanCodes
@ArjanCodes Жыл бұрын
Ha, good point!
@cosmicblack
@cosmicblack Жыл бұрын
damn, your videos are so great. I like them a lot. Thanks for your time
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful!
@Rapha5l
@Rapha5l Жыл бұрын
I still use .format() when I need cariage return or tab in my string ("/n","/t"). How do you manage to use that kind of special caracter with f-string? (I use f"{chr(10)}" but I think it is harder to read than "/n")
@kenhaley4
@kenhaley4 Жыл бұрын
Very nice! (and I thought I knew f-strings). By the way I oftern use the debugging feature with embedded calculations like so: f"{quantity * price = }". Very handy at times. There needs to be a "cheat sheet" showing all these options. (Is there?)
@engcisco
@engcisco Жыл бұрын
great video as usual, anytime frame for the next discount on your course?
@jainicz
@jainicz Жыл бұрын
I have been using f-strings for almost everything except (1) Dynamic formatting. Declare an unformatted string such that it can later be used in multiple places, replaced with different variables. (2) LaTeX or any script that involves a lot of curly brackets, "{" and "}". While it is possible to escape curly brackets in f-strings, I would prefer to keep my script concise.
@K1assh
@K1assh Жыл бұрын
wow, what a great QOL update. I've been still using the function, which would always end up looking like .format(name=name, age=age, screw=this). Which hurts my heart. This new way uses all the same operators that I am used to, so easy and much appreciated change.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful!
@patrickdallaire5972
@patrickdallaire5972 Жыл бұрын
Arjan, I love f-strings, dataclasses and other things that you show up. Unfortunately, where I work, people are still clinging to older versions of python... all the way back to 2.7... how do you convince people to migrate from 2.7 to 3.8 or later?
@copperfield42
@copperfield42 Жыл бұрын
cool, I didn't know some of these. And about the others form of string formatting, I use them if it is for templates if I use it multiples times and then just call .format on it, otherwise f-string for the win, and now when I check my old code I always change the old formats to f-string...
@epeleg
@epeleg Жыл бұрын
can the format be parametric? e.g. setting digits=4 and doing something like print(f"{123.345567:.digits}") ?
@adrianfebre7424
@adrianfebre7424 Жыл бұрын
My one and only format() use case is dynamically inserting variables into a large SQL query (in my case, large multi-line strings, perhaps 50 lines on average).
@alexanderpoplawski577
@alexanderpoplawski577 Жыл бұрын
In this case it would be safer to use the cursor execute method with parameters to prevent SQL injection.
@adrianfebre7424
@adrianfebre7424 Жыл бұрын
@@alexanderpoplawski577 Interesting! I hadn't thought to do that, but that's a great idea
@OMGnotThatGuy
@OMGnotThatGuy Жыл бұрын
I LOVE f-strings and I use them constantly, but I still managed to learn a few things from this video! I didn’t know f-strings could do time formatting and I didn’t know that the debugging f-strings would honor spaces. The only thing f-strings is missing is lazily evaluated or deferred f-strings. That’s the only reason I ever use format() or Template. It’d be nice to be able to pass lazy f-strings to functions or load them from configs.
@davidagnew8465
@davidagnew8465 Жыл бұрын
You can build an f-string using normal string methods, compile it using the compile function, and invoke the compiled version using eval(code, 'generated', 'eval'). It will run surprisingly fast.
@OMGnotThatGuy
@OMGnotThatGuy Жыл бұрын
@@davidagnew8465 Yeah, but using eval() seems pretty hackish and problematic, especially when you're dealing with strings that have quotes in them. It could become a nightmare to escape properly and eval has negative security implications. I avoid using it wherever possible, doubly so if any variables contain user input. What would be nice is if we had a way to tell python that an f-string was lazy when we defined it. Then it would evaluate when __str__ dunder is called. Something like putting f! instead of f: >>> template = f!"Number: {i:2}" >>> for i in range(8,11): 2 print(template) Number: 8 Number: 9 Number: 10
@eliavrad2845
@eliavrad2845 Жыл бұрын
​ @Jonathan Maybe a function is more appropriate for this kind of cases? template = lambda i: f"Number: {i:2}" for i in range(8,11): print(template(i)) def my_big_template(name, amount, currency): return f"{name} has deposited {amount:.2}{currency}" for name, amount in zip(people, deposits): print(template(name, amount, "$"))
@sjmarel
@sjmarel Жыл бұрын
Other formatting methods should be used in order for beginners to understand why f-strings are so awesome. Nice one again, Arjan!
@MattRose30000
@MattRose30000 Жыл бұрын
It's still useful to learn them as long as there is so much legacy code floating around on the web.
@sjmarel
@sjmarel Жыл бұрын
@@MattRose30000 you are completely right. As a matter of fact, the percentage style formatting is very much a common feature of many langs. Talking about legacy code, what's up with the new Int to Str conversion in python? It broke lots of libraries!
@paulwilliamson5985
@paulwilliamson5985 Жыл бұрын
What are best practices for using f-strings with internationalization?
@dawidskreczko
@dawidskreczko Жыл бұрын
Dziękujemy.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much Dawid!
@pacersgo
@pacersgo Жыл бұрын
Nice contents! I am also working in the Netherlands, and I found our Dutch computers expect “.” as the thousands separator and “,” as the decimal separator. Is there a quick way to print numbers in such a format?
@stevegrimes5105
@stevegrimes5105 Жыл бұрын
f"{str(your_variable).replace('.',',')} possibly?
@pacersgo
@pacersgo Жыл бұрын
@@stevegrimes5105 it is basically what I am doing now, by swapping the symbols. But I hope there is a more elegant, robust and flexible solution. Thanks bro!
@virtualraider
@virtualraider Жыл бұрын
The only thing I haven't been able to figure out how to do with f-strings that you can do with .format is how to expand a dictionary into the placeholders. i.e. don't think you can do this: ``` template = "my number is {number} {item}" values = dict(number=1, item='box') print(template.format(**values)) ``` Output: _my number is 1 box_
@ArjanCodes
@ArjanCodes Жыл бұрын
That's a good reason indeed to still use templates. You could also do this, but templates might still be easier: ``` FRUITS = [{"name": "Arjan", "fruit": "apples"}, {"name": "Paula", "fruit": "grapes"}] for obj in FRUITS: name, fruit = obj.values() print(f"{name} likes {fruit}.") ```
@CaptainCsaba
@CaptainCsaba Жыл бұрын
Would love a pendulum tutorial.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks for the suggestions, I've put it on the list.
@CaptainCsaba
@CaptainCsaba Жыл бұрын
@@ArjanCodes Thank you!
@robertlove2168
@robertlove2168 Жыл бұрын
At the start you showed how to print 800 as a hex number but it didn't look like 0x320. Is that possible with an f-string option?
@digiryde
@digiryde Жыл бұрын
What I learned today. We can all be 'leet' twice per day. But, seriously, another good instructional video.
@jace3789
@jace3789 Жыл бұрын
Thanks
@ArjanCodes
@ArjanCodes Жыл бұрын
You're Welcome!! :)
@SLHacks
@SLHacks Жыл бұрын
nice, didn't knew these.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks so much, glad the content is helpful! :)
@mehdicherifi6289
@mehdicherifi6289 Жыл бұрын
Hello pls can you provide a list of best python GUI frameworks? Thank you
@lungenbrotchen
@lungenbrotchen Жыл бұрын
I use f-strings for everything except If something has to be translated. Most of the projects I am working on are using pyqt and translation is done via qt linguist.
@juanbetancourt5106
@juanbetancourt5106 Жыл бұрын
4:10 is it possible to use f strings to change: "." as thousand separator and "," as decimal separator? thank you for sharing your knowledge Arjan!
@xlindvain
@xlindvain Жыл бұрын
that's my question as well
@alperengencoglu5367
@alperengencoglu5367 Жыл бұрын
You can check out the built-in "locale" module for that.
@michalbotor
@michalbotor Жыл бұрын
try this: ``` import locale val = 1_000_000 loc = locale.getlocale() print(loc) print(f'{val}') print(f'{val:,}') print(f'{val:,.02f}') print(f'${val:,.02f}') loc = locale.setlocale(locale.LC_ALL, 'de_DE') print(loc) print(f'{val}') print(f'{val:,}') print(f'{val:,.02f}') print(f'${val:,.02f}') ``` i used some online ide and it failed because most of them have only us_US installed. if you are on linux you can check what locale you have installed with the following command: ``` ➜ locale -a C C.UTF-8 en_US.utf8 POSIX ➜ ``` these are the one installed on the online ide that i've used.
@michalbotor
@michalbotor Жыл бұрын
there is probably also a way to display with locale the currency without hardcoded currency symbol; euros for germany
@sousaguiar
@sousaguiar Жыл бұрын
I use this way: print(f"The number is {8000:_.2f}".replace(".",",").replace("_","."))
@13junior62
@13junior62 Жыл бұрын
Maybe a little bit out of context :) but when I get a date info from an email using imaplib and email, it always come as utc. Can I change the timezone info and get the date as I intend to my own timezone or should I use f-string way or just manipulate the date object with timedelta(hours)? Thanks for all the efforts, it's been a joy to watch your videos so far :) (sorry for the English if I made some mistakes)
@SoWhatsAgain
@SoWhatsAgain Жыл бұрын
It's better to do with pytz rather than timedelta: import pytz localized_datetime = pytz.timezone(timezone).localize(datetime.datetime()) the list of available timezones you can find in pytz/__init__.py file
@13junior62
@13junior62 Жыл бұрын
@@SoWhatsAgain I will try that, thank you for the answer.
@_baco
@_baco Жыл бұрын
You had me at "1337", hahahaha!!!
@user-hf5be1zu7s
@user-hf5be1zu7s Жыл бұрын
Is there a sum up to all the subjects in the video? plz
@Gummibandet1
@Gummibandet1 Жыл бұрын
Is f-strings faster even if you don't actually print a variable? E.g. print(f"Hello World") vs print ("Hello World")?
@mehboobstudiophotography1283
@mehboobstudiophotography1283 Жыл бұрын
know what they tNice tutorialnk, and hopefully give so pointers. Thank you!
@Tweakimp
@Tweakimp Жыл бұрын
How do you like jupyter notebooks? This video could have shown the formatting in a notebook without switching between the tabs :)
@josephs4580
@josephs4580 Жыл бұрын
pylint complains at me when I use f-strings when passing a message to the logging module. Pylint(W1203:logging-fstring-interpolation) Is this warning bogus?
A Deep Dive Into Pathlib And The Magic Behind It
16:56
ArjanCodes
Рет қаралды 45 М.
ARRAYLIST VS LINKEDLIST
21:20
Core Dumped
Рет қаралды 46 М.
BRUSH ONE’S TEETH WITH A CARDBOARD TOOTHBRUSH!#asmr
00:35
HAYATAKU はやたく
Рет қаралды 20 МЛН
Joven bailarín noquea a ladrón de un golpe #nmas #shorts
00:17
顔面水槽をカラフルにしたらキモ過ぎたwwwww
00:59
はじめしゃちょー(hajime)
Рет қаралды 32 МЛН
Когда на улице Маябрь 😈 #марьяна #шортс
00:17
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 49 М.
A Deep Dive Into Date And Time In Python
19:39
ArjanCodes
Рет қаралды 40 М.
OLYMPIAD MATHS: Index Problem #youtube #maths #olympiad
3:10
FunOnlineMaths
Рет қаралды 17
5 Reasons Why You Should Use Type Hints In Python
13:54
ArjanCodes
Рет қаралды 103 М.
A Deep Dive Into Iterators and Itertools in Python
21:01
ArjanCodes
Рет қаралды 59 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
If You’re Not Using Python DATA CLASSES Yet, You Should 🚀
10:55
The Ultimate Guide to Writing Functions
24:31
ArjanCodes
Рет қаралды 176 М.
+10 CRAZY Ways To FORMAT Text In Python with F-Strings
9:15
Indently
Рет қаралды 19 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 355 М.
BRUSH ONE’S TEETH WITH A CARDBOARD TOOTHBRUSH!#asmr
00:35
HAYATAKU はやたく
Рет қаралды 20 МЛН