Why the result prints b'hello,Python!' ,when I use tensorflow?

Viewed 5796

the code is pinned underneath:

import tensorflow as tf

hello=tf.constant("hello,Python!")

sess=tf.Session()

print(sess.run(hello))

enter image description here

the current result is pinned underneath:

b'hello,Python!'

then the screenshot

so,what shall I do to drop the strange "b" before the current result?

1 Answers

As per Python 2.x documentation:

A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.

So in Python 3.x

bytes = b'...' literals = a sequence of octets (integers between 0 and 255)

It's not really there, it only gets printed. This should not cause any problem anywhere.

You can try decode("utf-8") to convert it to str.

Works for me

>>> print(out)
b'hello,Python!'
>>> out.decode('utf-8')
'hello,Python!'
Related