Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to do a bytes f-string?
#4
Not available. From the docs

Quote:A string literal with 'f' or 'F' in its prefix is a formatted string literal; see Formatted string literals. The 'f' may be combined with 'r', but not with 'b' or 'u', therefore raw formatted strings are possible, but formatted bytes literals are not.

And the pep for f-strings talks about the rationale here.

Quote:No binary f-strings

For the same reason that we don't support bytes.format(), you may not combine 'f' with 'b' string literals. The primary problem is that an object's __format__() method may return Unicode data that is not compatible with a bytes string.

Binary f-strings would first require a solution for bytes.format(). This idea has been proposed in the past, most recently in PEP 461 [11]. The discussions of such a feature usually suggest either

adding a method such as __bformat__() so an object can control how it is converted to bytes, or
having bytes.format() not be as general purpose or extensible as str.format().

Both of these remain as options in the future, if such functionality is desired.
Reply


Messages In This Thread
how to do a bytes f-string? - by Skaperen - Jul-15-2020, 05:34 AM
RE: how to do a bytes f-string? - by menator01 - Jul-15-2020, 07:16 AM
RE: how to do a bytes f-string? - by Skaperen - Jul-16-2020, 02:47 AM
RE: how to do a bytes f-string? - by bowlofred - Jul-16-2020, 04:19 AM
RE: how to do a bytes f-string? - by Skaperen - Jul-16-2020, 11:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 7,686 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  bytes f-string ? Skaperen 5 4,463 Jun-10-2021, 10:21 PM
Last Post: Gribouillis
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,355 May-02-2021, 03:45 PM
Last Post: Anldra12
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 18,743 May-08-2020, 04:26 AM
Last Post: bowlofred
  printing a bytes string Skaperen 2 2,453 Jul-21-2019, 03:42 AM
Last Post: Skaperen
  replace bytes with other byte or bytes BigOldArt 1 10,795 Feb-02-2019, 11:00 PM
Last Post: snippsat
  portion of the bytes to string ricardons 1 2,673 Mar-02-2018, 12:00 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020