+10 CRAZY Ways To FORMAT Text In Python with F-Strings

Sdílet
Vložit

Komentáře • 42

  • @Martin-delta
    @Martin-delta Před rokem +40

    The = Can be uset to print both variable names and their value.
    print(f"{foo=} {bar=}") - will print “foo= 1 bar= 2”.
    Useful for quick debugging.

    • @Indently
      @Indently  Před rokem +9

      I immediately realised after I uploaded the video that I missed that one

    • @Martin-delta
      @Martin-delta Před rokem +2

      @@Indently I even think I learned it from one of your previous videos. :)

    • @schlopping
      @schlopping Před rokem +1

      Correction: Not variable name, but expressions! e.g. f"{1+1=}" -> "1+1=2"

  • @FreihEitner
    @FreihEitner Před rokem +2

    This is VERY cool and handy info. I'm fairly new to coding in Python but I can already see some users for this.

  • @cirklare
    @cirklare Před rokem +10

    That's why you need to read the manual for anything you buy

    • @Indently
      @Indently  Před rokem +7

      I'm the kind of guy who buys the Lego™ Death Star, throws away the box, and builds something entirely unrelated.

  • @tuzaguilar4201
    @tuzaguilar4201 Před 4 měsíci +1

    Thanks! I was looking for something like this just a day ago.

  • @Chalisque
    @Chalisque Před 2 měsíci

    Something I like, when defining your own classes, is that whatever you write after the : (e.g. the 'turnip' in {a:turnip}) gets send to the object's __format__(self,formatstring) method. So it's very open-ended.

  • @532hariharareddyg5
    @532hariharareddyg5 Před rokem +1

    best explanation

  • @yaseralhalaby471
    @yaseralhalaby471 Před rokem

    Thank you for this video, it is very helpful

  • @manarmimi3073
    @manarmimi3073 Před rokem

    Thank you
    Good job.

  • @grzegorzryznar5101
    @grzegorzryznar5101 Před rokem +3

    1. Reduce amount of decimals is not the best word in that case, cause the number will be just rounded not cut off ;)
    2. Recently, I've discovered, that it is possible to add own format to class via __format__ magic method ;) And then it can be used in fstring for example

  • @emadelazhary2373
    @emadelazhary2373 Před rokem

    Thanks.

  • @trp
    @trp Před rokem

    This is so cool

  • @RetroGamingRandomness
    @RetroGamingRandomness Před rokem +1

    i saw these a couple weeks ago, but i didn't know about the last one

  • @EvokerKing
    @EvokerKing Před rokem +1

    6:55 could you show us in a separate video how you can make the part set up to handle that in a module similar to the way the datetime module handles it? i know it uses some kind of format function, but that is it.

  • @ChronicoOne
    @ChronicoOne Před rokem +1

    As someone who's written a function in Python to properly print out a date, I am grateful for your enlightenment. If you want to be enlightened about machine learning, tap my face😅

  • @Heavy_Lvy
    @Heavy_Lvy Před rokem

    How did you causally do the start of the video

  • @-awwolf-
    @-awwolf- Před rokem

    Is there a way to swap points and commas in floats, as it is written in Germany, for example?

    • @Indently
      @Indently  Před rokem

      Unfortunately, I don't know of any way of doing that with the simple {var:,} syntax, which is really annoying.

  • @richardboreiko
    @richardboreiko Před rokem +1

    Can you indicate AM or PM with the time?

  • @benfurstenwerth
    @benfurstenwerth Před rokem +3

    datetime.datetime.datetime lol made my morning! Thank you for that

  • @Lanxxe
    @Lanxxe Před rokem +1

    Is it possible to make a space " " the thousand separator with f-strings ?

    • @Martin-delta
      @Martin-delta Před rokem +3

      {:,d} is not locale aware. So the simplest way is just .replace(',', ' ')
      Or you can use the locale lib and override the default separator.
      import locale
      locale.resetlocale() # Load default locale
      locale._override_localeconv = {'thousands_sep': ' '} # Set space as thousand sep
      locale.format_string('%d', 123456789, grouping=True) => 123 456 789

  • @bernardcrnkovic3769
    @bernardcrnkovic3769 Před 2 měsíci

    i dont know why i laughed so hard at the start..

  • @zapp7746
    @zapp7746 Před rokem

    F strings are cool but Zapp's comments are cooler

  • @ruzibayevich1693
    @ruzibayevich1693 Před rokem

    I didn't feel how I lost my 9 mins , I didn't wanna watch it till the end actually 😂

  • @yibing8112
    @yibing8112 Před rokem

    watch video before: proficientin in python
    after: heard python

  • @howl2339
    @howl2339 Před rokem +1

    Bananas, mhhhhh

  • @Mulakulu
    @Mulakulu Před rokem +1

    Im still disappointed that we can't use a variable in place of that 20 for spacing

    • @Indently
      @Indently  Před rokem +1

      Yes you can, watch the entire video and you'll see how.

    • @Mulakulu
      @Mulakulu Před rokem +1

      @@Indently oh shoot. I'm spreading fake news. Thank you 👍

  • @nugget7236
    @nugget7236 Před rokem

    Today I learned that a scientific notation is not a error

  • @Lahmeinthehouse
    @Lahmeinthehouse Před rokem

    How is this useful?

    • @Indently
      @Indently  Před rokem

      What do you mean?

    • @Lahmeinthehouse
      @Lahmeinthehouse Před rokem

      @@Indently why would it be interesting to print something with a lot of spaces and characters? I just don’t see the value in it in general

    • @Indently
      @Indently  Před rokem +1

      Formatting is a preference. You might want to make some logs look nice to make it easier for a user (or even yourself) to understand.

    • @Lahmeinthehouse
      @Lahmeinthehouse Před rokem

      @@Indently oh, right! Good point, thank you ☺️

  • @sg8nj
    @sg8nj Před rokem

    print('{:*^10}'.format('Hi')) try this.
    Output: ****Hi****