如何测试字节字符串对象的单个字符?

How to test individual characters of a bytes string object?

假设我们要测试 b"hello" 的第一个字符是否是 b"h":

s = b"hello"
print(s[0] == b"h")      # False   <-- this is the most obvious solution, but doesn't work
print(s[0] == ord(b"h"))  # True, but not very explicit

测试字节串的一个字符(不一定是第一个字符)是否为给定字符(例如 b"h")的最标准方法是什么? (也许有关于此的官方 PEP 推荐?)

你可以做到 s[:1] == b"h".

(maybe there is an official PEP recommendation about this?)

如果您的意思是 PEP 8,那么据我所知,它没有针对 bytes 对象的任何建议。

由于访问第 n 个字节元素是 return 整数,我建议采用以下解决方案

s = b"hello"
print(s[0] == b"h"[0])  # True