存档:
2023 年 01 月 (1)
1、对象 python中判断对象的方式为type() ,但是字符串转换的方式却不太一样 字节转换成字符串,如下: bytes1 = b'i am bytes' # 是字节,验证类型 print(type(Bytes_str)) str_1 = Bytes_str.decode('utf-8') # str_1是字符串类型,转换类型 print(type(str_1)) # 验证类型 字符串转换成字节,如下: str2 = 'i am str' # 创建字符串,并查看类型 print(type(str2)) bytes1 = str2.encode() # 转换类型,并验证 print(type(bytes1)) bs = b'testing bytes' # 使用小 ‘b’ 也可将字符串转换成bytes(字节) print(type(bs)) # 验证类型