strip(rm)用来删除元素内的空白符:
rm对应要删除空白符的元素,当rm为空(strip())时删除所有元素的空白符
startswith、endswith用来查找开头或结尾条件的元素
例子:
1 li = ["alec", " aric", "Alex", "Tony", "rain"] 2 tu = ("alec", " aric", "Alex", "Tony", "rain") 3 dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"} 4 for i in li: 5 b = i.strip() 6 if (b.startswith("a") or b.startswith("A")) and b.endswith("c"): 7 print(b) 8 9 for i in tu: 10 b = i.strip() 11 if (b.startswith("a") or b.startswith("A")) and b.endswith("c"): 12 print(b) 13 14 for i in dic: 15 b = dic[i].strip() 16 if (b.startswith("a") or b.startswith("A")) and b.endswith("c"): 17 print(b)
上面代码:查找以 a或A开头并且以 c 结尾的所有元素,并输出
输出结果:
alec aric alec aric aric