Python中的字符串文字前的'b'字符做什么?

Consider the following examples,

考虑以下示例,

# variable declarations
test_str = 'string'
test_bytes = b'string'

# printing the types
print(type(test_str))
print(type(test_bytes))

Output

输出量

<class 'str'>
<class 'bytes'>

As per the above example, the prefix of 'b' character to a string, makes the variable of type bytes.

按照上面的示例,字符串中的'b'字符前缀使变量成为字节类型。

Before version 3, python always ignored the prefix 'b' and in the later version, bytes variable are always prefixed with ‘b’. They may contain ASCII characters, bytes with a numeric value of 128 or greater must be expressed with escapes.

在版本3之前,python始终忽略前缀'b',而在更高版本中,bytes变量始终以'b'作为前缀。 它们可能包含ASCII字符,数字值等于或大于128的字节必须用转义符表示。

The bytes are the actual data. Strings are an abstraction.

字节是实际数据。 字符串是一种抽象。

翻译自: https://www.includehelp.com/python/what-does-the-b-character-do-in-front-of-a-string-literal.aspx

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值