You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中print二进制处理机制、bytes函数原理及存储显示疑问

Python's print() and Binary Data Handling

Let’s break down your questions one by one, starting with how print() deals with binary data, then the bytes function, and finally your code examples and memory storage questions.

How print() Displays bytes Objects

When you pass a bytes object to print(), Python doesn’t output raw binary bits directly—it uses a human-readable format:

  • It adds a b prefix to explicitly signal this is a bytes type (not a regular string).
  • For byte values that map to printable ASCII characters (like letters, digits, or common symbols), it shows the character inside quotes.
  • For non-printable or non-ASCII bytes, it uses escape sequences (e.g., \x00 for null, \n for newline).

This balances clarity about the data type with readability for familiar characters.

Understanding the bytes() Function

The bytes() function creates an immutable sequence of bytes (each byte is an integer between 0 and 255). Here’s how it works in common scenarios:

  • Iterable of integers: bytes([100]) converts each integer in the list to a byte (values outside 0-255 throw an error).
  • String with encoding: bytes('d', 'ascii') encodes the string into bytes using the specified encoding.
  • Literal shortcut: b'd' is a shorthand for creating a bytes object containing the ASCII byte for 'd' (decimal value 100).

Breaking Down Your Code Examples

Let’s walk through your snippets and clear up the confusion around "binary assignment."

Example 1: b'd' and a = bytes([100])

Running this code:

print(type(b'd'))  # Output: <class 'bytes'>
print(b'd')        # Output: b'd'

a = bytes([100])
print(type(a))     # Output: <class 'bytes'>
print(a)           # Output: b'd'

Both b'd' and bytes([100]) create identical bytes objects. The single byte inside has a decimal value of 100, which maps to the ASCII character 'd'—so print() uses b'd' as the readable representation.

Clarifying the "Binary Assignment" Misconception

You mentioned assigning "binary 1100100" as a = 1100100, but that’s a common mix-up:

  • 1100100 is just a decimal integer (110,010 in base 10), not a binary type. Its type() would be int, not bytes.
  • To write a binary literal integer, prefix it with 0b: a = 0b1100100 (this equals decimal 100). Even then, type(a) is still int, not bytes.

If you want a bytes object from the binary value 0b1100100, use a = bytes([0b1100100])—this is identical to bytes([100]), so print(a) will still output b'd'.

How bytes Objects Are Stored in Memory

  • Under the hood, all computer data is stored as binary (0s and 1s). A bytes object is a sequence of 8-bit values—so the byte for 'd' (decimal 100) is stored as the 8-bit binary 01100100 (leading zero to fill 8 bits).
  • Hexadecimal representations (like 0x64 for decimal 100) are just human-readable shorthand—computers don’t store data in hex. Hex is useful because each digit represents 4 bits, making it easier to read than long binary strings.
  • Python’s bytes object doesn’t "store" in hex or decimal—those are just ways to display the underlying binary data to humans.

内容的提问来源于stack exchange,提问作者KMG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:07:28