🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Bytes

AI & DATA SCIENCE // bytes

bytes is Python's built-in, immutable type for a sequence of raw byte values (integers from 0 to 255), used for binary data and encoded text.

Syntax

b = b"hello"
b = bytes([104, 105])
s.encode("utf-8")

Deep Dive Course

A bytes object stores a fixed sequence of small integers, 0-255, rather than Unicode characters, making it the right type for binary data — file contents opened in binary mode, network payloads, image data — as opposed to str, which represents text. Converting between the two is explicit: str.encode() turns text into bytes using a given encoding (utf-8 by default), and bytes.decode() turns bytes back into text.

1Understanding Bytes

A bytes object stores a fixed sequence of small integers, 0-255, rather than Unicode characters, making it the right type for binary data — file contents opened in binary mode, network payloads, image data — as opposed to str, which represents text. Converting between the two is explicit: str.encode() turns text into bytes using a given encoding (utf-8 by default), and bytes.decode() turns bytes back into text.

💡

Always encode/decode with an explicit encoding argument like 'utf-8' — relying on the platform default can silently produce different results on different operating systems.

editor.html
data = "caf\u00e9".encode("utf-8")
print(data)
print(data.decode("utf-8"))
localhost:3000

2Practical Example

Here is a real-world application of Bytes showing how it is used in production Python code.

editor.html
raw = bytes([72, 101, 108, 108, 111])
print(raw)
print(raw.decode("ascii"))
localhost:3000

3Best Practices

Follow these guidelines when working with Bytes:

1. Use bytes (or bytearray) for binary data and network/file I/O, not str

2. Always specify an explicit encoding, like 'utf-8', when calling encode()/decode()

3. Use bytearray instead of bytes when you need a mutable byte sequence, such as building up a buffer incrementally

⚠️

Tip: Always encode/decode with an explicit encoding argument like 'utf-8' — relying on the platform default can silently produce different results on different operating systems.

editor.html
data = "caf\u00e9".encode("utf-8")
print(data)
print(data.decode("utf-8"))
localhost:3000

Examples

Example 01Basic Usage
data = "caf\u00e9".encode("utf-8")
print(data)
print(data.decode("utf-8"))
Example 02Advanced Example
raw = bytes([72, 101, 108, 108, 111])
print(raw)
print(raw.decode("ascii"))

Best Practices

  • Use bytes (or bytearray) for binary data and network/file I/O, not str
  • Always specify an explicit encoding, like 'utf-8', when calling encode()/decode()
  • Use bytearray instead of bytes when you need a mutable byte sequence, such as building up a buffer incrementally

Interview Question

What's the difference between str and bytes in Python 3, and why can't you concatenate them directly?

Hint: Think about what each type actually represents.

str represents text as a sequence of Unicode code points; bytes represents raw binary data as a sequence of small integers. Python 3 deliberately keeps them as separate, incompatible types — concatenating a str and a bytes object raises a TypeError — forcing an explicit encode() or decode() call at the boundary, which avoids the silent, ambiguous text/binary mixing that caused many bugs in Python 2.

Exercises

MediumPractice using Bytes in a real scenario.
View Solution
data = "caf\u00e9".encode("utf-8")
print(data)
print(data.decode("utf-8"))

Frequently Asked Questions

What's the difference between str and bytes in Python 3, and why can't you concatenate them directly?

str represents text as a sequence of Unicode code points; bytes represents raw binary data as a sequence of small integers. Python 3 deliberately keeps them as separate, incompatible types — concatenating a str and a bytes object raises a TypeError — forcing an explicit encode() or decode() call at the boundary, which avoids the silent, ambiguous text/binary mixing that caused many bugs in Python 2.

Related Functions

stringsbytearraystr.encode()