python-small-examples

@author jackzhenguo
@desc 
@date 2019/2/15

14 字符串转字节  

字符串转换为字节类型

In [12]: s = "apple"                                                            

In [13]: a = bytes(s,encoding='utf-8')   
In [14] a 
Out[14]: b'apple'

# 转化后a变为字节序列,bytes类型,
# 并且每个字符都被转化为数值,如下所示
In [15]: for i in a: 
    ...:     print(i)                                                                       
97
112
112
108
101
[上一个例子](/python-small-examples/md/13.html) [下一个例子](/python-small-examples/md/15.html)