Str vs Bytes in Python: Key Differences and Usage
str represents text as Unicode characters, while bytes represents raw binary data as sequences of bytes. Use str for readable text and bytes for binary files or network data.Quick Comparison
Here is a quick side-by-side comparison of str and bytes in Python.
| Aspect | str | bytes |
|---|---|---|
| Type of data | Text (Unicode characters) | Binary data (raw bytes) |
| Data format | Sequence of Unicode code points | Sequence of 8-bit integers (0-255) |
| Literal syntax | Enclosed in single/double quotes, e.g. 'hello' | Prefixed with b, e.g. b'hello' |
| Mutability | Immutable | Immutable |
| Common use cases | Text processing, user input, display | File I/O, network communication, binary protocols |
| Encoding/decoding | No encoding needed to use as text | Must decode to str to read as text |
Key Differences
str in Python stores text as Unicode characters, which means it can represent letters, symbols, and emojis from any language. It is designed for human-readable text and supports many string operations like concatenation, searching, and formatting.
bytes, on the other hand, stores raw 8-bit values. It is used when working with data that is not text, such as images, audio files, or network packets. Since bytes is a sequence of numbers, it cannot be directly printed as readable text without decoding.
To convert between them, you encode a str into bytes using an encoding like UTF-8, and decode bytes back to str. This is important because computers store text as bytes, but Python lets you work with text easily using str.
Code Comparison
text = 'Hello, world! 👋' print(text) print(type(text)) encoded = text.encode('utf-8') print(encoded) print(type(encoded))
Bytes Equivalent
data = b'Hello, world! \xf0\x9f\x91\x8b' print(data) print(type(data)) decoded = data.decode('utf-8') print(decoded) print(type(decoded))
When to Use Which
Choose str when you work with text that humans read or write, such as messages, file paths, or user input. It is easy to manipulate and display.
Choose bytes when handling raw data like files, images, or network communication where data is not guaranteed to be text. You must encode and decode when switching between these types.
Always remember: str is for text, bytes is for raw data.
Key Takeaways
str for human-readable text and bytes for raw binary data.str stores Unicode characters; bytes stores sequences of 8-bit integers.encode() (str to bytes) and decode() (bytes to str).bytes is essential for file I/O, network data, and binary protocols.