0

I know it's been asked on this site, but I admit I didn't understand the answer to this question.

In Python 3, there is the 'binary literal', which is expressed by prefixing a string with a b. E.g.:

b'hello world'

I understand that this turns the string into a bytes object. But, a string is already a sequence of bytes underneath, isn't it? So what do this actually do for us? Also, using what encoding does this encode the string to bytes?

Please clear things up for me. I am referring specifically to Python 3.

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • Check the [docs](http://docs.python.org/3.3/reference/lexical_analysis.html#string-literals) `Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.` – thefourtheye Mar 13 '15 at 16:46
  • *"But, a string is already a sequence of bytes underneath, isn't it?"* In python 3, absolutely not. Strings are interacted with as *sequences of characters*, unlike say C++ where they are simply *sequences of bytes that represent characters in some way*. This allows Strings in python to, for the most part, mask the underlying encoding. `str[0]` will always be the first *character* in the text string, `str[1]` the second, and so on. In a dumb sequence-of-bytes string like C++'s, `string[1]` is always the second *byte*, which may or may not be the second character depending on encoding. – aruisdante Mar 13 '15 at 16:48
  • @thefourtheye So writing `b'a a a'` actually yields the byte sequence `97 97 97`? – Aviv Cohn Mar 13 '15 at 19:12

0 Answers0