Strings vs. Bytes

We'll cover the following...

Bytes are bytes; characters are an abstraction. An immutable sequence of Unicode characters is called a string. An immutable sequence of numbers-between-0-and-255 is called a bytes object.

Press + to interact
by = b'abcd\x65'
print (by)
#b'abcde'
print (type(by) ) #②
#<class 'bytes'>
print (len(by) ) #③
#5
by += b'\xff' #④
print (by)
#b'abcde\xff'
print (len(by)) #⑤
#6
print (by[0]) #⑥
#97
Press + to interact
by = b'abcd\x65'
print (by[0] = 102) #⑦
# File "/usercode/__ed_file.py", line 2
# print (by[0] = 102) #\u2466
# ^
#SyntaxError: keyword can't be an expression

① To define a bytes object, use the b'' “byte literal” syntax. Each byte within the byte literal can be an ASCII character or an encoded hexadecimal number from \x00 to \xff (0–255).

② The type of a bytes object is bytes.

③ Just like lists and strings, you can get the length of a bytes object with the built-in len() function.

④ Just like lists and ...

Access this course and 1400+ top-rated courses and projects.