1. iter()
用来帮助判断某元素是否在字符串中。 iter() returns an iterator for the given object. The iter() method creates an object which can be iterated one element at a time. These objects are useful when coupled with loops like for loop, while loop. 举例如果查找某一个元素是否在字符串中,可以将此字符串变成可迭代对象。注意只能往前,不能回头,如图: 用法可以配合for loop ,while loop:
s
= 'ahd'
t
= 'ahbgdce'
[_
in t
for _
in s
] -> [True, True, True]
可以配合all()食用
s
= 'ahd'
t
= 'ahbgdce'
all(_
in t
for _
in s
) -> True
2. find()&index()
用来查找某元素的位置。 find()比index()好用,因为find()若木有,则返回-1,index()则会报错 还有从右边往左开始查找的rfind()和rindex()
3. __getitem__
根据位置找元素,主要用在类里。纯为学习一下__getitem__。
参考: