0

Using Micropython sockets I received a message which contained the following string:

b'xxx/yyy'

I don't know how the "b" got there, or what it is !!!

I can also enter this using an input statement

    x = input('Enter:')
>>> b'xxx/yyy'
    print(x)
>>> b'xxx/yyy'
    print (len(x))
>>> 7
    L = []
    L = x.split('/')
>>> TypeError: can't convert 'str' object to str implicitly

There must be a simple explanation to this, but I can not see it.

Is this some new type of variable ???

larsks
  • 277,717
  • 41
  • 399
  • 399
user1530405
  • 447
  • 1
  • 8
  • 16
  • The b before the string shows that it is a byte string. http://stackoverflow.com/questions/6224052/what-is-the-difference-between-a-string-and-a-byte-string – Gary Bake Jan 14 '17 at 08:36
  • Thank you, That explains it... – user1530405 Jan 16 '17 at 17:23
  • 2
    Possible duplicate of [What does a b prefix before a python string mean?](https://stackoverflow.com/questions/2592764/what-does-a-b-prefix-before-a-python-string-mean) – larsks Nov 11 '17 at 22:41

2 Answers2

3

It states that it is a utf-8 decoded string enclosed in a bytearray!!

RLN
  • 48
  • 6
1

You need to convert this value. So instead of:

print(x)

try

print(x.decode('utf-8'))

Tikky
  • 1,253
  • 2
  • 17
  • 36