1

I have read that in Python, a bytestring is a sequence of bytes

To completely understand the above statement, I have written the following code:

s = 'hello'
b = s.encode()
list(b)

The above code is returning following output:

[104, 101, 108, 108, 111]

My question is why b here is returning sequence of decimal values instead of returning sequence of bytes?

Can you please help me understand this concept using some example?

sjakobi
  • 3,546
  • 1
  • 25
  • 43
meallhour
  • 13,921
  • 21
  • 60
  • 117
  • A byte is just a number between 0 and 255 – Barmar Oct 06 '21 at 17:50
  • Python represents *a* byte as an integer in the range 0..255. – MisterMiyagi Oct 06 '21 at 17:50
  • If `byte is a number between 0 and 255` then what is the difference between a byte and `unicode code point`? – meallhour Oct 06 '21 at 17:51
  • 1
    The range of unicode codepoints is considerably larger than 0-255 ... – snakecharmerb Oct 06 '21 at 17:52
  • Neither a byte nor a unicode code point *are* integers. They are *represented* as integers. That doesn't make them equivalent. Just because Jason down the street is 23 years old and Becky from work has 23 Dollars doesn't mean Jason's age is Becky's cash. – MisterMiyagi Oct 06 '21 at 17:53
  • 2
    It's simply a design decision. The developers could have made `bytes` iterators return length-1 `bytes` objects and they could have made `str` iterators return `int` objects representing Unicode code points. But `bytes` typically represent numbers (data) and `str` typically represent text, so it made more sense to the developers to implement it they way they did. – Mark Tolonen Oct 06 '21 at 18:16
  • can you please help me understand this concept using some example? – meallhour Oct 06 '21 at 18:43

0 Answers0