THIS Is My Favourite F-String Trick In Python

Поделиться
HTML-код
  • Опубликовано: 21 окт 2023
  • This is my favourite f-string trick in Python #code #programming #python

Комментарии • 131

  • @butwhatif2489
    @butwhatif2489 8 месяцев назад +252

    New python dlc looks crazy ngl

    • @69k_gold
      @69k_gold 8 месяцев назад +7

      😂😂😂

    • @sourtil7158
      @sourtil7158 7 месяцев назад +10

      Python: C edition

  • @UnbackedMuffn
    @UnbackedMuffn 6 месяцев назад +8

    I learned this from the replit 100 days of code. You can do it without the character as well. You have left: < right> and center^.

  • @gayedemiray
    @gayedemiray 8 месяцев назад +72

    omg this is amazing fr

  • @jerry-yu7yi
    @jerry-yu7yi 8 месяцев назад +93

    if you could it would be cool if you include the python version from now on in your description

    • @irishRocker1
      @irishRocker1 8 месяцев назад +16

      Python 3 😂

    • @hunorfekete7413
      @hunorfekete7413 8 месяцев назад +9

      ​@@irishRocker1but it may be 3.12 cus they buffed f strings there

    • @Spencer-wc6ew
      @Spencer-wc6ew 8 месяцев назад +8

      ​@@hunorfekete7413this wasn't a part of the recent f string improvements. F strings have had this ability since they were added in Python 3.6 (which was in 2016).

    • @Andrew-stay
      @Andrew-stay 7 месяцев назад

      ​@@hunorfekete7413it works on 3.11.6 at least

    • @nojukuramu
      @nojukuramu 6 месяцев назад +1

      Just assume he is using the latest version

  • @billyhorn7886
    @billyhorn7886 8 месяцев назад +9

    If you don't type a symbol after the colon it just uses blank space

    • @yodasoja2011
      @yodasoja2011 3 месяца назад

      Thank you! That's the one question I had from this great Short

  • @AyushSingh-yq4so
    @AyushSingh-yq4so 8 месяцев назад +20

    Can also use center(), ljust(), rjust()

    • @dp.229
      @dp.229 8 месяцев назад +3

      Yea, but using f-string formatting is faster and feels cooler 😁

    • @Loki-
      @Loki- 7 месяцев назад +1

      Then what are use cases for your suggestions that would be better than fstring? I know one pro is the syntax for yours is easier to remember, but I'm wondering efficiencies or other benefits.

    • @AyushSingh-yq4so
      @AyushSingh-yq4so 7 месяцев назад +1

      @@Loki- Don't know about efficiency, it's just straightforward.

  • @subhankarsamanta7783
    @subhankarsamanta7783 2 месяца назад

    Incredible!

  • @Strong256
    @Strong256 8 месяцев назад +2

    I learnt that from Rust and I was surprised months ago when I realized it was also in Python 😂🎉

  • @hlubradio2318
    @hlubradio2318 2 месяца назад

    Nice for menus

  • @digitalmachine0101
    @digitalmachine0101 5 месяцев назад

    Good information

  • @KiraIsGod
    @KiraIsGod 6 месяцев назад

    Wow, this looks super useful!

  • @fredflintstone8048
    @fredflintstone8048 7 месяцев назад

    Love all your tips.

  • @curtheisler1200
    @curtheisler1200 7 месяцев назад

    I was making a web scraper just yesterday and wanted to represent the page via a tree diagram.
    now I know how I can print that tree in a reasonable way!!

  • @swarajaggarwal
    @swarajaggarwal 6 месяцев назад

    Nice way of making loading bars

  • @user-ym7ss6xb3j
    @user-ym7ss6xb3j 7 месяцев назад +2

    var: str ................... lol

  • @Roadkill_1
    @Roadkill_1 8 месяцев назад +3

    So my assessment code could have been 10x cleaner

  • @haritmohansaxena732
    @haritmohansaxena732 6 месяцев назад +1

    Would help in logging, wondering if we could have it js

  • @argentum3919
    @argentum3919 3 месяца назад

    Before you know it, they'll be adding a graphic representation of your screen and you will use your mouse and a couple of number fields to format.

  • @ieatsoap6389
    @ieatsoap6389 7 месяцев назад

    You making that damn asterisk pyramid would be more better If used that when I was in highschool.

  • @xjk.-
    @xjk.- 8 месяцев назад

    impeccable

  • @nojukuramu
    @nojukuramu 6 месяцев назад

    I actually use it to display prints in table

  • @williambreeze2659
    @williambreeze2659 2 месяца назад

    This is cool.

  • @PatrickSteil
    @PatrickSteil 7 месяцев назад +2

    Awesome! Python continues to amaze me!!

  • @TrustifierTubes
    @TrustifierTubes 5 месяцев назад

    If you were surprised by that, I wonder what you thought at the f stands for. 😊

  • @NeilTruick
    @NeilTruick 6 месяцев назад

    Sweet!

  • @JeersNX
    @JeersNX 6 месяцев назад

    YEAH

  • @RamkrishanYT
    @RamkrishanYT 7 месяцев назад +2

    leftpad has left the chat

    • @Wardyworlds
      @Wardyworlds 7 месяцев назад

      What editor kicked leafpad out? I've been learning and using leafpad but just installed PyCharm

  • @josephsgambelluri5427
    @josephsgambelluri5427 7 месяцев назад +1

    Up arrow aka carat

  • @bilalmsd07
    @bilalmsd07 5 месяцев назад

    use cases should be describe as well.

  • @abhishankpaul
    @abhishankpaul 6 месяцев назад

    Felt nice to see features no one cares about 😅

  • @tresvecesno7071
    @tresvecesno7071 6 месяцев назад +1

    Wait, is that a python with types? Nice

  • @jimmorrison2657
    @jimmorrison2657 5 месяцев назад

    This ultra rare requirement gets it own syntax in the language. Use a method with a descriptive name.

  • @sobbski2672
    @sobbski2672 8 месяцев назад

    The ^ seems pretty useful

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 8 месяцев назад

    You've already told these in another video entitled as "+10 Crazy Ways to...." 😅

  • @hallrules
    @hallrules 7 месяцев назад

    Why tf does python have a built in left pad too

  • @yousefsrour3316
    @yousefsrour3316 8 месяцев назад

    Is this a new feature?

  • @Efebur
    @Efebur 8 месяцев назад +3

    can you use space as the symbol?

    • @infiniteplanes5775
      @infiniteplanes5775 6 месяцев назад

      Yes! If you don’t include a symbol to print, it will use a space by default

  • @vorkutavorkutlag
    @vorkutavorkutlag 6 месяцев назад

    so zfill but for strings

  • @Pawlo370
    @Pawlo370 8 месяцев назад

    You forgot about date

  • @mtalhakhalid1679
    @mtalhakhalid1679 5 месяцев назад

    Irs a varlous operstor

  • @danielwtf97
    @danielwtf97 7 месяцев назад

    This might be incredible useful for date formating e.g. 1->01

    • @funkdefied1
      @funkdefied1 7 месяцев назад +2

      The datetime module has better formatting options with the strftime() function

    • @danielwtf97
      @danielwtf97 7 месяцев назад

      @@funkdefied1 ahh good to know, thanks!

    • @Lucky9_9
      @Lucky9_9 7 месяцев назад

      The fastest way to learn a different approach to code syntax is to state your intention, and your current iteration. Inevitably people will contribute their own approaches.. and one day on the future, someone desperate will come across your hidden treasure of collected approaches and thank their lucky stars because you will have saved their entire project. 😂🫶

  • @NightDay1
    @NightDay1 4 месяца назад +1

    What is( var: str = 'Python')
    What is this called i didn't find it on google.

    • @julians.2597
      @julians.2597 4 месяца назад

      a variable declaration for a variable called `var` with a "type hint" denoting its type as string.
      Python doesn't by default actually do anything with these but there's type checkers like mypy who can statically typecheck your code.

  • @Hydroxyium
    @Hydroxyium 7 месяцев назад

    That's sick! 0 padding numbers is going to be so much easier

    • @akupython
      @akupython 5 месяцев назад

      :02d zero pads a digit. 01,02, etc. Change the 2 as required for pad length.

  • @leprozorijc7988
    @leprozorijc7988 7 месяцев назад

    I know what you can't do with fstrings: you can't use them in python 3.5

  • @mr.k8660
    @mr.k8660 8 месяцев назад +6

    Didn't know there is static typing in python 😮

    • @fusionsid270
      @fusionsid270 7 месяцев назад +5

      they are just type hints, don't affect the code in any way or provide performance benefits however it's really useful if you're coding in an editor cause you get type checking and autocomplete

    • @SkyyySi
      @SkyyySi 7 месяцев назад +1

      There isn't, type annotations are just hints for programmers and editors

    • @mr.k8660
      @mr.k8660 7 месяцев назад

      @@fusionsid270 oh ok

    • @noahdwhitney
      @noahdwhitney 6 месяцев назад

      ⁠@@fusionsid270how does this work? Is it like typescript checking the type at compile time? I’m assuming at runtime python doesn’t care? I’m not familiar with python at all I’m more of a C# & go guy

    • @fusionsid270
      @fusionsid270 6 месяцев назад

      @@noahdwhitney yep python doesn't care at runtime, think of them as just comments. They are used to annotate your code and make it easier to read. Also some editors have support for checking the types so if you typed a variable as int but later assign it to a string, The editor will shout at you. However as the python interpreter completely ignores these, even if your ide is shouting at you, the code will still run. You can also get linters such as flake8 to check your code and if you want something closer to a typed language, you can use mypy. It's somewhat similar to typescript's compiler so it'll check your code first then run it

  • @voodookiidoo
    @voodookiidoo 8 месяцев назад +2

    damn kids readin the docs now thats a breakthrough

  • @crashmatrix
    @crashmatrix 7 месяцев назад

    fstring soup is already an issue as it is, let's make it worse with embedded fstring evaluation!

  • @FlopD0g0
    @FlopD0g0 7 месяцев назад

    Im not going to remember this but it looks cool 😂

    • @Wardyworlds
      @Wardyworlds 7 месяцев назад

      You might now I've replied to you and you can come back to the post.

    • @FlopD0g0
      @FlopD0g0 7 месяцев назад +1

      ​@@Wardyworldsthank you

  • @ohhellnooooo8233
    @ohhellnooooo8233 5 месяцев назад

    What’s the use case ?

    • @julians.2597
      @julians.2597 4 месяца назад

      I used it to line up rows in a CLI I was asked to build a while ago. basically lines up individual columns in a table and makes them a lot nicer to look at.

  • @wystrzalowy
    @wystrzalowy 4 месяца назад

    ai showed it yo me yesterday

  • @bigzigtv706
    @bigzigtv706 7 месяцев назад

    What could you ever need to do this for

  • @teo11300
    @teo11300 6 месяцев назад

    Why is it ‘var: str = ‘ instead of just ‘var = ‘?

    • @julians.2597
      @julians.2597 4 месяца назад +1

      it's a variable declaration for a variable called `var` with a "type hint" denoting its type as string.
      Python doesn't by default actually do anything with these but there's type checkers like mypy who can statically typecheck your code.

  • @Wineblood
    @Wineblood 5 месяцев назад

    This isn't f string specific

  • @nitingoswami1959
    @nitingoswami1959 8 месяцев назад +1

    What is this =>
    var: str = "Python"
    is python become statically typed?

    • @Dralexgon
      @Dralexgon 8 месяцев назад +3

      You can add a type to a variable in python but it's only for information. It has no impact on execution.
      Exemple :
      myVar : str = "test"
      myVar = 8
      print(myVar)
      >>>8
      Without exception

    • @gesuchter
      @gesuchter 8 месяцев назад +3

      It's only a type hint and this feature came with Python 3.5 and therefore has been there for ages

    • @johnstarfire
      @johnstarfire 8 месяцев назад

      It’s only a ‘reminder’ of what the coder wanted to use that variable for, so I think you should use it always if you want to remember what were you doing

    • @hidude1354
      @hidude1354 8 месяцев назад +1

      this does nothing to the python compiler/interpreter, it's just for helping remember types mainly for methods

    • @johnstarfire
      @johnstarfire 8 месяцев назад

      it's the same as a comment # str, to say it should be a string, but if you change it later to an int for example no error will be raised

  • @JacobSeeger
    @JacobSeeger 8 месяцев назад +6

    What would this be used for practically 😂

    • @LeonardTopno
      @LeonardTopno 8 месяцев назад +1

      Exactly my thought.

    • @timothypasket5598
      @timothypasket5598 8 месяцев назад

      One thought would be leading zeroes on an ID

    • @0x00AE1C9B
      @0x00AE1C9B 8 месяцев назад +8

      Formatting stdout data like logs

    • @funkdefied1
      @funkdefied1 7 месяцев назад

      Pretty CLIs

    • @Loki-
      @Loki- 7 месяцев назад +1

      Another tool in the toolbox. Which is all of learning programming.

  • @earthbender731
    @earthbender731 8 месяцев назад +2

    why do you do var: str = "var1" instead of var = "var1"

    • @phatboislym
      @phatboislym 8 месяцев назад +5

      that's a type annotation, he's stating the data type at declaration
      it helps his text editor check for errors and provide suggestions

    • @earthbender731
      @earthbender731 8 месяцев назад

      @@phatboislym does it work in vscode?looks like good practice,im gonna start using that

    • @phatboislym
      @phatboislym 8 месяцев назад +1

      @@earthbender731yes, it does

    • @Slackow
      @Slackow 8 месяцев назад +2

      @@earthbender731it's not entirely necessary in this case since the IDE will recognize it anyway, type hints are much more useful for method parameters over anything else, or for more complex types.

    • @welly888
      @welly888 8 месяцев назад +2

      I have been learning and practicing Python for 1 month and i cant even understand a single line this guy types 😢

  • @TehJumpingJawa
    @TehJumpingJawa 6 месяцев назад

    Congratulations Python had devised a String butchering syntax even more unreadable than Regex.

  • @mateuszzdyb3547
    @mateuszzdyb3547 6 месяцев назад

    great, will never use it anyways :D

  • @jstello
    @jstello 8 месяцев назад

    Is this for 3.12?

    • @julians.2597
      @julians.2597 4 месяца назад +1

      3.6, with the introduction of fstrings

  • @davidofug
    @davidofug 5 месяцев назад

    This is incredible!

  • @-es2bf
    @-es2bf 8 месяцев назад

    If you have a favorite string you need to have a life outside of your job.

  • @sixthsense844
    @sixthsense844 8 месяцев назад +1

    May I ask, what is the use of this?

    • @Slackow
      @Slackow 8 месяцев назад +2

      Aligning fixed width columns over multiple lines, typically

  • @ducgia1493
    @ducgia1493 4 месяца назад

    Child's programming language 😂.

    • @Indently
      @Indently  4 месяца назад

      I'm sorry you see it that way :)

  • @ImKinoNichtSabbeln
    @ImKinoNichtSabbeln 5 месяцев назад

    And once again, the Python community find something Perl-ish "cool".
    Interesting...

  • @alexisdamnit9012
    @alexisdamnit9012 7 месяцев назад

    It’s cool but is it Pythonic? 🤨

  • @theLowestPointInMyLife
    @theLowestPointInMyLife 7 месяцев назад

    Copied from rust

  • @ventsislavminev
    @ventsislavminev 5 месяцев назад

    Very cool and very useless

  • @Tenly2009
    @Tenly2009 7 месяцев назад

    Today you should also learn that there is no “up arrow”. It’s called a “carat”.
    If you’re going to call it an “up arrow” in a video meant to educate others, you may as well also call the “tilde” a “squiggly line” 🤦🏻‍♂️ 😒

    • @Indently
      @Indently  7 месяцев назад +1

      I don’t regret what I said, nor have any intention of changing it. Over 95% of my followers are non-native English speakers. I’m not an English teacher, I want everyone around the world to understand as much as possible for that is the best thing in terms of communication. I would love to use the correct terms for everything, but it doesn’t matter in this context, go to university 🤣

    • @Tenly2009
      @Tenly2009 7 месяцев назад

      @@IndentlyOf course you don’t think you did anything wrong. Arrogant narcissist’s never accept constructive criticism or corrections as they already think they are perfect. 🙄
      I misspoke in my original reply because some keyboards do in fact contain an “up arrow” key - which makes your statement in this video even more incorrect and more inaccurate.
      And you really think that an ESL viewer has more chance of knowing “tilde” than “carat”? 🤦🏻‍♂️ I’ll bet that 80% or more of your ESL viewers know English better than you do - but keep talking down to them and assuming they’re dumb and see how they like it. 😆

    • @Indently
      @Indently  6 месяцев назад +1

      I see you have a particular talent in insulting people on the internet.

    • @Tenly2009
      @Tenly2009 6 месяцев назад

      @@Indently Well, I started with constructive criticism - but you wouldn’t take it and instead pretended your mistake was intentional and the right thing to do (when it clearly wasn’t).
      So then, instead of the insults, what would you suggest I say to the next arrogant narcissist I come across that pretends a mistake they made was an intentional choice? 🤔

  • @JuanPablodelaTorre
    @JuanPablodelaTorre 7 месяцев назад +1

    Python devs are so cute. Learning that f strings are just *printf* strings in Python flavor.

    • @Indently
      @Indently  7 месяцев назад +1

      Flexing in the shorts section on social media is also pretty cute

    • @JuanPablodelaTorre
      @JuanPablodelaTorre 7 месяцев назад

      @@Indently I know 🥰🥰

    • @luishenrique
      @luishenrique 7 месяцев назад

      kkkkk