What does the 'b' character do in front of a string literal?
The b
prefix creates a bytes
object from a string literal in Python. It converts every character into its ASCII equivalent, which is useful for dealing with binary data (such as in network communication or binary files) instead of text.
Example:
Using non-ASCII characters in bytes
Besides ASCII characters, the b
prefix can handle non-ASCII characters with escape sequences.
Example:
This example demonstrates how to incorporate non-ASCII characters in bytes
objects either by escape sequences
or encoding
.
Bytes vs. str: A Tale of Two Types
In Python, str
is a sequence of Unicode characters, while bytes
is a sequence of 8-bit values or raw binary data. The encode()
and decode()
methods transform str
to bytes
and vice versa, respectively.
Example:
Appropriate times for a 'b' appearance
Here are the key scenarios that beckon for the use of b
prefix:
- Binary file operations
- Data transmission over a network
- Data processing with binary-data-friendly APIs or libraries
Example:
The 'b' and 'r' duo
Combine b
with r
(raw) to form byte literals where escape sequences, such as \n
or \t
, are interpreted as raw text.
Example:
Bytes and Unicode in harmony
b
and u
play well together in the Python 2.x syntax, where u
indicates a Unicode string. In Python 3, strings are intrinsically Unicode, and b
is used to specify a byte string.
Example:
Proceed with caution: Potential issues
- Information loss: During transformation from
str
tobytes
, use the correct encoding to prevent losing information. - TypeError: A
TypeError
arises when you perform an operation or concatenatebytes
andstr
objects directly. - Presentation of byte literals: Byte literals with printable ASCII characters show as ASCII, while others appear as escape sequences.
In code:
Here's the right way to do it:
Was this article helpful?