Strings vs. Bytes
Explore the differences between strings and bytes in Python 3. Understand their distinct types, immutability, and how to convert bytes to strings and vice versa using encoding and decoding. This lesson helps you manipulate and work with these data types confidently in your Python code.
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.
① 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 strings, you can use the +operator to concatenate bytes objects. The result is a new bytes object.
⑤ ...