python常用数据类型
int
str
a = '1234'
print(type(a))
输出结果:
<class 'str'>
bytes
b = b'123'
print(type(b))
输出结果:
<class 'bytes'>
类型转换
str => bytes
方式一
s = bytes(a, encoding='utf8')
print(type(s))
输出结果:
<class 'bytes'>
需要注意的是bytes()函数本身返回的也是bytes类型数据
方式二:
s =a.encode('gbk')
print(type(s))
输出结果:
<class 'bytes'>
bytes => str
方式一:
s = str(b, encoding='utf8')
print(type(s))
输出结果:
<class 'str'>
方式二:
s = b.decode('gbk')
print(type(s))
输出结果:
<class 'str'>
网友评论