Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to do a bytes f-string?
#1
how to do a bytes f-string?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Not sure if this is what you're asking

h = b'hello'
print(h)
print(type(h))

print(f'{h.decode()}')
h = h.decode()
print(type(h))
Output:
b'hello' <class 'bytes'> hello <class 'str'>
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
i don't follow.

i wanted to do:
x = b'xyzzy'
s = bf'foo{x}bar'
but that is a syntax error.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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
#5
my suggestion is for bytes formatting getting a non-bytes unicode character to raise an exception.

the other thing i have a need for is a non-literal f-string. that is, a way to process a regular string that has contents that would be work in a literal f-string, but do it with that non-literal string value.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,878 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  bytes f-string ? Skaperen 5 4,349 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,231 May-02-2021, 03:45 PM
Last Post: Anldra12
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 18,638 May-08-2020, 04:26 AM
Last Post: bowlofred
  printing a bytes string Skaperen 2 2,376 Jul-21-2019, 03:42 AM
Last Post: Skaperen
  replace bytes with other byte or bytes BigOldArt 1 10,657 Feb-02-2019, 11:00 PM
Last Post: snippsat
  portion of the bytes to string ricardons 1 2,626 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