码迷,mamicode.com
首页 > 编程语言 > 详细

python中strip、startswith、endswith

时间:2017-09-09 22:40:42      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:条件   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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!