The letter S in a light blue, stylized speech bubble followed by SpeakBits
SpeakBitsThe letter S in a light blue, stylized speech bubble followed by SpeakBits
Trending
Top
New
Controversial
Search
Groups

Enjoying SpeakBits?

Support the development of it by donating to Patreon or Ko-Fi.
About
Rules
Terms
Privacy
EULA
Cookies
Blog
Have feedback? We'd love to hear it!

bytes: The Lesser-Known Python Built-In Sequence And Understanding UTF-8 Encoding

thepythoncodingstack.com
submitted
a year ago
byjustadevtoprogramming

Summary

What is a bytes object? It's a built-in data type. It's similar to a string, but with a different name. It can be used to create a new data type, such as a new type of string. It is a type of data, not a type.

ASCII is one of the encodings used to translate characters into numbers. Each element in a bytes object represents an integer between 0 and 255. The first 128 Unicode characters match the ASCII characters. But there are only 128 more numbers left in a single byte (128-255)

The accented letter é is represented by two bytes. The value of the first of these bytes is c3 in hexadecimal, which is 195 in decimal. The last two bytes combined represent the last character, é. But what do the integers 195 and 169 represent?

There are six bytes in greeting_bytes, even though there are only two characters in the string. You can distinguish each byte as they start with \x to indicate they're escape sequences representing hexadecimal numbers. The first character is represented by the first three bytes out of the six.

Hexadecimal is base 16, which means there are 16 digits instead of 10. Two digits in hexadecimo represent 256 numbers (from 00 to FF, which is 0 to 255 in decimal) Binary follows the same pattern as decimal and hexadeCimal but is in base 2.

Thank you to all those who supported me with a one-off donation recently. This means a lot and helps me focus on writing more articles. The Python Coding Book is available (Ebook and paperback) The NumPy Mindset is available as an Early Release.

 menu cliff drop drop-off jigsaw puzzle candle taper wax light-0
10

3 Comments

3
joseph
a year ago
By the time I've had to work with bytes like this, I'm doing some crazy optimization. I've always wondered at what point do other developers reach for really specific data types like this
1
iareunique
a year ago
I always struggle to find use cases for these aside from academic examples
1
justadevOP
a year ago
It's always nice to have options for when you do run into that case but most people will not find that