Print without b' prefix for bytes in Python 3

PythonStringPython 3.x

Python Problem Overview


How do I print a bytes string without the b' prefix in Python 3?

>>> print(b'hello')
b'hello'

Python Solutions


Solution 1 - Python

Use decode:

>>> print(b'hello'.decode())
hello

Solution 2 - Python

If the bytes use an appropriate character encoding already; you could print them directly:

sys.stdout.buffer.write(data)

or

nwritten = os.write(sys.stdout.fileno(), data)  # NOTE: it may write less than len(data) bytes

Solution 3 - Python

If the data is in an UTF-8 compatible format, you can convert the bytes to a string.

>>> print(str(b"hello", "utf-8"))
hello

Optionally, convert to hex first if the data is not UTF-8 compatible (e.g. data is raw bytes).

>>> from binascii import hexlify
>>> print(hexlify(b"\x13\x37"))
b'1337'
>>> print(str(hexlify(b"\x13\x37"), "utf-8"))
1337
>>> from codecs import encode  # alternative
>>> print(str(encode(b"\x13\x37", "hex"), "utf-8"))
1337

Solution 4 - Python

According to the source for bytes.__repr__, the b'' is baked into the method.

One workaround is to manually slice off the b'' from the resulting repr():

>>> x = b'\x01\x02\x03\x04'

>>> print(repr(x))
b'\x01\x02\x03\x04'

>>> print(repr(x)[2:-1])
\x01\x02\x03\x04

Solution 5 - Python

To show or print:

<byte_object>.decode("utf-8")

To encode or save:

<str_object>.encode('utf-8')

Solution 6 - Python

I am a little late but for Python 3.9.1 this worked for me and removed the -b prefix:

print(outputCode.decode())

Solution 7 - Python

Use decode() instead of encode() for converting bytes to a string.

>>> import curses
>>> print(curses.version.decode())
2.2

Solution 8 - Python

It's so simple... (With that, you can encode the dictionary and list bytes, then you can stringify it using json.dump / json.dumps)

You just need use base64

import base64

data = b"Hello world!" # Bytes
data = base64.b64encode(data).decode() # Returns a base64 string, which can be decoded without error.
print(data)

There are bytes that cannot be decoded by default(pictures are an example), so base64 will encode those bytes into bytes that can be decoded to string, to retrieve the bytes just use

data = base64.b64decode(data.encode())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsdaauView Question on Stackoverflow
Solution 1 - PythonsdaauView Answer on Stackoverflow
Solution 2 - PythonjfsView Answer on Stackoverflow
Solution 3 - PythonFrankView Answer on Stackoverflow
Solution 4 - PythonMateen UlhaqView Answer on Stackoverflow
Solution 5 - Pythonhassanzadeh.sdView Answer on Stackoverflow
Solution 6 - Pythonsimo54View Answer on Stackoverflow
Solution 7 - PythonArkelisView Answer on Stackoverflow
Solution 8 - PythonGustavo10DestroyerView Answer on Stackoverflow