3

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?

2
  • My description may be a little bit puzzling, actually, after I ran the code, the head of the result showed a "b" which confused me a lot. Commented Sep 27, 2017 at 4:24
  • The b character prefix signifies that Hello, TensorFlow! is a byte string Commented Sep 27, 2017 at 4:34

1 Answer 1

6

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!'
Sign up to request clarification or add additional context in comments.

1 Comment

This is a temporary solution. So, need to change it every time. Is there any permanent solution for this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.