0
0
PythonComparisonBeginner · 3 min read

Str vs Bytes in Python: Key Differences and Usage

In Python, 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.

Aspectstrbytes
Type of dataText (Unicode characters)Binary data (raw bytes)
Data formatSequence of Unicode code pointsSequence of 8-bit integers (0-255)
Literal syntaxEnclosed in single/double quotes, e.g. 'hello'Prefixed with b, e.g. b'hello'
MutabilityImmutableImmutable
Common use casesText processing, user input, displayFile I/O, network communication, binary protocols
Encoding/decodingNo encoding needed to use as textMust 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

python
text = 'Hello, world! 👋'
print(text)
print(type(text))

encoded = text.encode('utf-8')
print(encoded)
print(type(encoded))
Output
Hello, world! 👋 <class 'str'> b'Hello, world! \xf0\x9f\x91\x8b' <class 'bytes'>
↔️

Bytes Equivalent

python
data = b'Hello, world! \xf0\x9f\x91\x8b'
print(data)
print(type(data))

decoded = data.decode('utf-8')
print(decoded)
print(type(decoded))
Output
b'Hello, world! \xf0\x9f\x91\x8b' <class 'bytes'> Hello, world! 👋 <class 'str'>
🎯

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

Use str for human-readable text and bytes for raw binary data.
str stores Unicode characters; bytes stores sequences of 8-bit integers.
Convert between them using encode() (str to bytes) and decode() (bytes to str).
bytes is essential for file I/O, network data, and binary protocols.
Always choose the type based on whether your data is text or raw binary.