From the course: Python Essential Training

Bytes

- [Instructor] So here's one we haven't covered yet. The Python byte object. So in day-to-day programming, you won't often be working with it. It's common behind the scenes. It's data that gets passed around in a program but it's rarely manipulated or modified directly. As we all know, computers store information as ones and zeros on the disk. When Python loads that data, there's information that tells Python what the type is, if it's a string, an int, some sort of class with properties, et cetera. In some cases though, all you really want is data. Some random series of ones and zeros. And so there's the bytes object. What kind of data is the bytes object? Who cares. What type is it? Well, it's a bytes object. It's a sequence of data and that's all Python needs to know. The bytes object is commonly used for streaming files or transmitting texts without knowing what the encoding is. You'll see it pop up a lot in Python libraries. So let's take a look at how to recognize and use the bytes object. So first, let's create one. Now this looks like we're casting the integer four to a bytes object, but not so fast. What this does is it actually creates an empty bytes object that's four bytes long. Each byte here is represented with a /x followed by two hexadecimal or base 16 numbers. So remember that two hexadecimal numbers is 256 possibilities which is the same as two to the power of eight or eight bits. And of course there are eight bits to a byte. Okay, so this is four bytes long. And this b here is used to differentiate bytes from a regular string. So if you see something printed out and it has the b in front of it, it's a bytes object. Let's make a bytes object with actual data in it. So to do this, let's do a fun emoji here. The eye roll. Okay. We need to tell Python what the type is. So we're going to do utf-8. So in order to create the bytes object, you need to tell it what the type is of the thing that you're trying to encode so it knows how to find and isolate the data. And in this case, emojis are encoded with utf-8 which is a type of Unicode transformation format. So once it knows the format of the thing you're feeding it, it can represent the data correctly. Let's call this smiley bytes. Okay. And there's the bytes for that emoji. Now how do we go the other way? Take a bytes object and represent it as a string again. For this, you need the decode function. And again, you need to pass the format in. So decode utf-8. And there we go. Bytes objects are immutable like tuples. So if we want bytes data that we can modify, we can use something called a byte array. Let's actually just take this, change it to a byte array. The syntax is very similar. We can see this is a byte array now. We can treat this smiley bytes object a lot like a string, including modifying a specific byte value using the string slice notation. So let's take this value at the last position. This is hexadecimal 84, and we can take this and that's at index three there. So we're going to say it's 85. Let's see what that is. So how do we get hexadecimal 85? We can use the int library, remember that? So let's take 85 in base 16, and then we set that. And then we want to decode it, see what we get. Decode utf-8. It looks like a shrugging emoji. So that's how to find, detect, use, and modify bytes.

Contents