Python Forum
[Solved] Getting python's default 'printed' byte-string as string ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Getting python's default 'printed' byte-string as string ?
#1
Question 
Answer: str() or repr() Blush
--- --- --- --- ---
So if one uses
print('bytestr:test-0', bytestr)
on some byte string.
You get this
Output:
bytestr:test-0 b'\x81\xff\xffscale\x00\x0f\x00\x00\x80@\x00\x89PNG...
as output.

There are of course plenty of decoding options.
But ... what I like to get is 'exactly' that "\x81\x..." printed string in string form. (For direct further processing. Mainly debug related.)
Is that possible?

(Figure I could use some tmp file ... well maybe, not tried that yet. Copying it from the I/O terminal is not really workable of course.)
Reply
#2
bytestr.hex()?
Reply
#3
No, not looking for some hex notation. I got that covered.
Truly looking for an 'exact' match (or as close as possible) of what I get in the I/O console.

Tried
bytestr.decode("ascii", errors="backslashreplace")
. But that will eat the "\x00" bytes.

Only thing that currently comes to mind is potentially processing the byte string per byte. And using
.decode("ascii")
(maybe) error checking to flip the notation depending on what I want as output.
Reply
#4
Are you trying to get what print(bytestr) displays as a str? Have you tried str(bytestr)? Does that give you what you want? If not, have you tried repr(bytestr)?

It would help to know what you are trying to do. If you want a str object that matches what you get from print(bytestr), str() or repr() will work fine. If you want to save bytes to a file and restore to a bytes object from the file, use a binary file.
Reply
#5
Darn. Totally forgot about
repr()
... which did the job.
Solved.
Reply
#6
repr(bytestr) and str(bytestr) return the same value. bytes.__str__(x) probably returns bytes.__repr__(x) or vice-versa.
the_bytes = bytes(range(256))  # Make bytes with every possible value.
str_bytes = str(the_bytes)
repr_bytes = repr(the_bytes)
print(str_bytes == repr_bytes)
Output:
True
When you print(bytestr), print calls bytestr.__str__(), not bytestr.__repr__(). __repr__() is used when you call repr(), or is used by the __str__() method for some classes such as List or Tuple.
x = "Hello, World!"
y = [x]

print(x, y)
Output:
Hello, World! ['Hello, World!']
Notice that quotes are added when printing x as part of y. __repr__() adds the quotes, __str__() does not.
Reply
#7
Aha. I see. Thanks.

I think I did tried "str(...)" at some point ... but only one time with some bad".decode()" code in it. And kinda gave up on "str()" from there. Blush

nts: remember to never try to test two different things at the same time.
Reply
#8
Quote:No, not looking for some hex notation. I got that covered.
Truly looking for an 'exact' match (or as close as possible) of what I get in the I/O console.

Tried
1 bytestr.decode("ascii", errors="backslashreplace")
But that will eat the "\x00" bytes.

Fussy about your food? Don't want to eat the null bytes? Use repr()!

x = bytes("é", encoding="utf8") # x = b'\xc3\xa9'
string = 'déjà vu'
string_bytes_ascii = string.encode("ascii", "backslashreplace") # b'd\\xe9j\\xe0 vu'
string_bytes_utf8 = string.encode("utf8", "backslashreplace") # b'd\xc3\xa9j\xc3\xa0 vu'
bd = list(string_bytes_utf8) # [100, 195, 169, 106, 195, 160, 32, 118, 117]
# add null bytes to the list above
binary_data = bytes([00, 100, 195, 169, 106, 195, 160, 32, 118, 117, 00])
binary_data.decode(encoding="utf-8") # 'déjà vu'
z = binary_data.decode(encoding="utf-8", errors="backslashreplace")
print(z) # nothing # \x00 represents end of string? Or end of file?
# wanna see the null bytes?
repr(z) # "'\\x00déjà vu\\x00'"
Reply
#9
Use decoding:
b = b'hello'
s = b.decode('utf-8')
buran write Apr-06-2026, 11:12 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing a string containing newlines meijeru 3 69 Mar-23-2026, 03:20 AM
Last Post: deanhystad
  python re.finditer returns a null string when expecting a None result arbiel 8 802 Jan-11-2026, 02:57 AM
Last Post: Pedroski55
  Spaces in string Mallard 9 1,070 Dec-19-2025, 01:04 PM
Last Post: Mallard
  Convert any Python expression to a string voidtrance 2 1,129 Jun-23-2025, 07:06 AM
Last Post: DeaD_EyE
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,524 May-28-2025, 07:56 AM
Last Post: Winfried
  Return a string or byte object from Enum class? Calab 5 1,959 May-14-2025, 05:21 PM
Last Post: snippsat
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,672 May-01-2025, 03:43 PM
Last Post: Winfried
  Get the string after a specific char JanJan 5 1,799 Apr-30-2025, 05:04 AM
Last Post: snl_9527
  TypeError: string indices must be integers deneme2 2 1,412 Feb-14-2025, 12:23 AM
Last Post: deneme2
  How do I parse the string? anna17 8 3,298 Feb-13-2025, 07:08 AM
Last Post: michaeljordan

Forum Jump:

User Panel Messages

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