-1

I'm trying to create a byte string, but it seems to be just a regular character string. What am I doing wrong here?

example

byteStr = b'some string'
byteStr #'some string'
utfStr = 'some string'.encode('utf-8')
utfStr #'some string'
byteStr == utfStr #True
Eric
  • 489
  • 5
  • 16
  • Your bytestring is created correctly; just because it's equal to a charstring doesn't mean that's wrong. I won't answer because I don't know how bytestrings work but comparing bytestrings and charstrings will (usually) give `True`. – hyper-neutrino Sep 29 '17 at 14:02
  • @HyperNeutrino updated, i was expecting the print of byteStr to show a b before the first quote, and the equivalency test to fail – Eric Sep 29 '17 at 14:03
  • @HyperNeutrino i see, also, when i run type(byteStr) i get back . Is that correct? – Eric Sep 29 '17 at 14:04
  • @anonymousPerson....thanks for the random downvote – Eric Sep 29 '17 at 14:09

1 Answers1

1

If you're trying to create a byte array in Python 2, it's called a bytearray. Python 2 does not have a byte string.. The b in front of the str is ignored in Python 2, meaning 'hello' == b'hello'

Try this:

>>> f = b'f'
>>> type(f)
<type 'str'>

Now, it's important to remember that u'f' == 'f':

>>> h = u'f'
>>> f == h
True
>>> type(h)
>>> <type 'unicode'>
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • Ahhh, I see. The tutorial I'm following is using python3 which explains the difference. Thank you! – Eric Sep 29 '17 at 14:08
  • as a quick follow up question, why would type(uftStr) in my example return instead of ? It seems as though the .encode('utf-8') function didnt work – Eric Sep 29 '17 at 14:16