[Python-Dev] Subtle difference between f-strings and str.format()

Serhiy Storchaka storchaka at gmail.com
Wed Mar 28 15:12:06 EDT 2018


28.03.18 22:05, Paul Moore пише
> I can't imagine (non-contrived) code where the fact that a is
> formatted before b is evaluated would matter, so I'm fine with option
> 3.

If formatting a and evaluating b both raise exceptions, the resulting 
exception depends on the order.

    $ ./python -bb
     >>> a = b'bytes'
     >>> '{}{}'.format(a, b)
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    NameError: name 'b' is not defined
     >>> f'{a}{b}'
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    BytesWarning: str() on a bytes instance


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <[Python-Dev] Subtle difference between f-strings and str.format()

	
  • Next message (by thread): [Python-Dev] Subtle difference between f-strings and str.format()
  • Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

  • More information about the Python-Dev mailing list

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant