字符串的查找与检查
index方法
index 是一种方法 不是函数
在字符串中找到所要求的 最低索引 (最近的 最左边) 返回索引的值
找不到就报错
例:
str = ‘asd’
str.index(‘a’)
输出结果 0
find 和 rfind 方法
find 从左往右找 返回先找到的索引值 找不到 返回-1
def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
"""
return 0
rfind 从左往右 返回最高找到的 索引值 找不到 返回 -1
def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
B.rfind(sub[, start[, end]]) -> int
Return the highest index in B where subsection sub is found,
such that sub is contained within B[start,end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
"""
return 0
count 方法
返回所查找的 在字符串中出现的次数 找不到 返回 0
def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
B.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of subsection sub in
bytes B[start:end]. Optional arguments start and end are interpreted
as in slice notation.
"""
return 0
其他方法
判断是否全由数字组成的方法isdigit
endswith 判断是否以指定的结尾
capitalize
istitle
title
endswith
isalpha
isdecimal 是否是小数
islower
replace
strip 删除字符串首尾空格 若给出指定字符 就删除指定字符
列表
1 表达形式
list1 = []
2 列表元素可以是不同的数据类型
3 列表可以 嵌套 例 : list1 = [1,2,[1,2,3,‘aa’]]
4 提取列表中的列表 例:
list1 = [1,2,3,[‘a’,‘b’,‘c’]]
例如 取 b
for i in list1[3][1]:
print(i)
列表的切片
可迭代对象
就是每个元素都能提取出来就是可迭代对象