标签:条件 nbsp tar span int 删除元素 python 并且 lex
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
python中strip、startswith、endswith
标签:条件 nbsp tar span int 删除元素 python 并且 lex
原文地址:http://www.cnblogs.com/zhangzengqiang/p/7499339.html