美文网首页
python数据类型及其转换

python数据类型及其转换

作者: mr_酱 | 来源:发表于2019-01-21 09:56 被阅读7次

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'>

相关文章

网友评论

      本文标题:python数据类型及其转换

      本文链接:https://www.haomeiwen.com/subject/wlwzeftx.html