str,list,tuple 都是序列,序列即是有序的
序列的共同操作:
1、通过序号访问数据
>>> 'hello world'[0]
'h'
>>> [1,2,3,4,'abc'][2]
3
>>> (2,3,'python',4)[2]
'python'
2、切片操作
>>> [1,2,3,4,'abc','python'][-3:-1]
[4, 'abc']
>>> 'hello world'[0:2]
'he'
>>> (1,2,3,4,'abc','python')[-3:-1]
(4, 'abc')
3、+ 、 * 操作
>>> 'hello' + 'world'
'helloworld'
>>> 'hello world'*3
'hello worldhello worldhello world'
4、in, not in 操作
>>> 'h' in 'hello world'
True
>>> 'python' in [1,2,3,4,'abc','python']
True
>>> 'python' not in [1,2,3,4,'abc','python']
False
5、len() ,max() , min() 操作
>>> len('hello world')
11
>>> len([1,2,3,4,'abc','python'])
6
>>> len((1,3,4,'abc','python'))
5
>>> max('hello world')
'w'
>>> min('hello world')
' '
>>> max([1,2,3,4,'abc','python'])
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
max([1,2,3,4,'abc','python'])
TypeError: '>' not supported between instances of 'str' and 'int'
>>> min([1,2,3,4])
1
>>> max((1,2,3,4))
4
查询’w‘的ascii的位置
>>> ord('w')
119
>>> ord(' ')
32
>>> ord(1)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
ord(1)
TypeError: ord() expected string of length 1, but int found