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?

roshan_nazareth
  • 311
  • 5
  • 16
DawnZhang
  • 55
  • 2
  • 6
  • 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. – DawnZhang Sep 27 '17 at 04:24
  • The `b` character prefix signifies that `Hello, TensorFlow!` is a [byte string](https://stackoverflow.com/questions/6224052/what-is-the-difference-between-a-string-and-a-byte-string) – call-in-co Sep 27 '17 at 04:34

1 Answers1

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!'
Amarpreet Singh
  • 2,242
  • 16
  • 27
  • This is a temporary solution. So, need to change it every time. Is there any permanent solution for this? – Emalka Feb 19 '19 at 05:33