首页 > 编程语言 > 详细

[python]bytes和str

时间:2017-06-18 20:39:00      阅读:453      评论:0      收藏:0      [点我收藏+]
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a="Hello World"
>>> b=a.encode("utf-8")
>>> b
b‘Hello World‘
>>> b[0]
72
>>> b[:1]
b‘H‘
>>> a[0]
‘H‘
>>> a[:1]
‘H‘
>>> type(b[0])
<class ‘int‘>
>>> c = [i for i in range(10)]
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c[0]
0
>>> c[:1]
[0]
>>>

bytes的元素都是range(256)的元素,bytes的切片都是bytes类型。

str的元素都是str(python没有char的概念),str的切片还是str类型。

列表的元素是元素的类型,列表的切片还是列表。

*所以说str是特殊的列表,bytes是特殊的str?

[python]bytes和str

原文:http://www.cnblogs.com/sigai/p/7045180.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!