Defining a Literal bytes Object
In this lesson, you’ll practice making a literal bytes
object. A bytes literal is defined in the same way as a string literal with the addition of a 'b'
prefix.
Single, double, or triple quoting mechanisms can be used. Only ASCII characters are allowed, and any character value greater than 127 must be specified using an appropriate escape sequence. The 'r'
prefix can be used to disable processing of escape sequences:
>>> a = b'spam egg bacon'
>>> a
b'spam egg bacon'
>>> type(a)
<class 'bytes'>
>>> c = b"Contain embedded 'single' quotes"
>>> c
b"Contain embedded 'single' quotes"
>>> type(c)
<class 'bytes'>
>>> t = b'''This bytes object contains "double" and 'single' quotes!'''
>>>type(t)
<class 'bytes'>
>>> t
b'This bytes object contains "double" and \'single\' quotes!'
>>> a = b'spam\xddegg'
>>> a
b'spam\xddegg'
>>> type(a)
<class 'bytes'>
>>> a[4]
221
>>> a[5]
101
>>> a[6]
103
>>> a[7]
103
>>> a = rb'spam\xddegg'
>>> a
b'spam\\xddegg'
>>> len(a)
11
>>> b = b'spam\xddegg'
>>> len(b)
8
00:00
For this video, you’re going to work on defining a literal bytes
object. So, how do you do that? A bytes
literal object is defined similarly to a string literal.
00:10
The biggest change is that it requires an additional lowercase b
as a prefix as you’re defining the object. Single, double, or triple quoting-mechanisms, just like they can be used in strings, can be used here also.
00:22 One unique thing is only ASCII characters are allowed, so any character value that’s greater than 127 must be specified using an appropriate escape sequence.
00:34
The lowercase r
prefix can be used to disable processing of escape sequences. I’ll have you create a bytes
object called a
. To start it you’ll use b
and then type out your string the same way you would before.
00:54
But instead of creating a string, what you’ve created here is a bytes
object, and you can check that by using type()
. So, other examples—you can use double quotes ("
), in case you need to contain embedded single quotes ('
) in your literal bytes
object.
01:17 Or you can also use the triple-quoting technique.
01:33
In that case, it will put in the escape characters automatically for you. Only ASCII characters can be included, so if you wanted to include something above the value of 127
, you would need an escape character, and that would be '\x'
and then two hexadecimal characters.
01:52
Hexadecimal goes from 0
all the way to the letter f
, so the highest value would be ff
. So you could put like '\xdd'
and then 'egg'
. And then in this case, you can see it included inside of your bytes
literal object.
02:12
And if you were to access that character, the fourth index within that bytes
object is a numeric value of 221
—again, over 127—to indicate this particular character. And index 5
would be the next letter, which is 'e'
, which is 101
, versus 6
, which is 103
, so 7
should be 103
also. They’re both the letter 'g'
.
02:40
If you were to use an r
at the beginning of your bytes
literal object, it’s going to disable the processing of any escape sequences.
02:50
So if you were to put in that '\xddegg'
, you actually will see that it’s escaped the '\'
automatically. And this length is different now—it’s 11
characters. Whereas the previous version
03:13
has only 8
. So it’s including all of these characters by being a raw byte literal—in this case, removing the escape sequence that would allow you to create that character that’s above 127. So that r
prefix can be used if you want to disable the processing of the escape sequences. Next up, how to define a bytes
object using the built-in bytes()
function.
Dan Bader RP Team on Aug. 2, 2020
The
bytes
object is one of the core built-in types for manipulating binary data. A bytes object is an immutable sequence of single byte values. Each element in abytes
object is a small integer in the range0
to255
.
Check out the tutorial linked under Supporting Material for more info: realpython.com/python-strings/#bytes-objects
D Parry on Aug. 2, 2020
Cheers!
tonypy on April 11, 2023
“So you could put like ‘\xdd’ and then ‘egg’. And then in this case, you can see it included inside of your bytes literal object. And if you were to access that character, the fourth index within that bytes object is a numeric value of 221—again, over 127—to indicate this particular character.”
I’m trying to understand whether there was a specific logic in using \xdd. The character with asciii value 221, hex DD, is Ý (Latin capital letter Y with acute). In the context of spam eggs that doesnt make any sense. Thoughts?
Become a Member to join the conversation.
D Parry on Aug. 2, 2020
A rookie question here:
Could you please explain in which situations bytes literal objects are used? There is some sort of logic and explanation when it comes to tuples, lists, dictionaries and strings but I could not find the answer for Bytes. (the purpose, the difference)
Thanks for anyone taking the time to answer it. :)