您的位置:首页 > 编程语言 > Python开发

python str bytes转换

2016-06-04 19:10 429 查看
1. 构造str bytes

str_var = 'hello world'
bytes_var = b'hello world'

print(type(str_var))
print(type(bytes_var))


output:

<class 'str'>

<class 'bytes'>

2. 构造str bytes
str_var = str('hello world')
bytes_var = bytes('hello world', encoding = 'UTF8')

print(type(str_var))
print(type(bytes_var))
output:

<class 'str'>

<class 'bytes'>

3. str bytes 转换

str_var = b'hello world'.decode(encoding = 'UTF8')
bytes_var = 'hello world'.encode(encoding = 'UTF8')

print(type(str_var))
print(type(bytes_var))
output:

<class 'str'>

<class 'bytes'>

4. str bytes 转换

str_var = str(b'hello world')
bytes_var = bytes('hello world', encoding = 'UTF8')

print(type(str_var))
print(type(bytes_var))
output:

<class 'str'>

<class 'bytes'>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: