# Python bytes类型示例代码
# 创建一个bytes对象
example_bytes = b'Hello, world!'
print(example_bytes) # 输出: b'Hello, world!'
# 访问bytes对象中的元素
print(example_bytes[0]) # 输出: 72 (H的ASCII码)
# 遍历bytes对象
for byte in example_bytes:
print(byte) # 输出每个字符的ASCII码值
# 将bytes对象转换为字符串
example_string = example_bytes.decode('utf-8')
print(example_string) # 输出: Hello, world!
# 将字符串转换为bytes对象
example_bytes_from_string = 'Hello, world!'.encode('utf-8')
print(example_bytes_from_string) # 输出: b'Hello, world!'
# 使用bytes函数创建bytes对象
example_bytes_from_list = bytes([72, 101, 108, 108, 111])
print(example_bytes_from_list) # 输出: b'Hello'
b或bytes()函数可以创建bytes对象。decode()方法可以将bytes对象转换为字符串,使用encode()方法可以将字符串转换为bytes对象。bytes()函数来创建bytes对象。上一篇:c++调用python库
下一篇:sorted函数python用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站