164

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

>>> print(b'hello')
b'hello'
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
sdaau
  • 36,975
  • 46
  • 198
  • 278
  • related: [How to write bytes to a file in Python 3 without knowing the encoding?](https://stackoverflow.com/q/4290716/4279) – jfs Sep 08 '17 at 15:17

8 Answers8

169

Use decode:

>>> print(b'hello'.decode())
hello
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
sdaau
  • 36,975
  • 46
  • 198
  • 278
  • 2
    How to do this by default, I mean, is it bad to use `utf-8` by default? I don't want to use the `.decode('utf-8')` everytime I print something. – Shubham A. Jan 10 '18 at 14:09
29

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
jfs
  • 399,953
  • 195
  • 994
  • 1,670
26

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
Frank
  • 2,375
  • 1
  • 19
  • 17
24

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
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • 8
    Side note: I don't think any of the other answers *truly* answer the question. – Mateen Ulhaq Jul 29 '19 at 06:42
  • I think I would agree: your solution, namely `repr(x)[2:-1]`, produces a `str` object that will print as wish. In particular, `repr(b'\x01')[2:-1]` returns the string `\\x01`, while `decode()` will return `\x01` which does not work as one would wish with `print()`. To be even more explicit, `print(repr(b'\x01')[2:-1])` will print `\x01` while `print(b'\x01'.decode())` will not print anything. – Antoine Sep 18 '19 at 13:54
  • Alternatively, ```print(repr(b"\x01".decode()))```will print `'\x01'` (a string including the single quotes ), so that ```print(repr(b"\x01".decode())[1:-1])``` prints `\x01` (a string without the single quotes ). – Antoine Sep 18 '19 at 14:04
9

To show or print:

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

To encode or save:

<str_object>.encode('utf-8')
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
hassanzadeh.sd
  • 3,091
  • 1
  • 17
  • 26
4

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

print(outputCode.decode())
simo54
  • 218
  • 5
  • 16
1

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())
1

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

>>> import curses
>>> print(curses.version.decode())
2.2
Arkelis
  • 913
  • 5
  • 12