python字符串相关操作

1、判断字符串是否以某个字符或者字符串开头:

1 >>> c="as_as_ddd_Fge_f"
2 >>> print c.startswith('f')
3 False
4 >>> print c.startswith('as')
5 True
6 >>> print c.startswith('a')
7 True
8 >>> print c.startswith('as_')
9 True
View Code

2、判断字符串是否以某个字符或者字符串结束:

1 >>> c="as_as_ddd_Fge_f"
2 >>> print c.endswith('f')
3 True
4 >>> print c.endswith('fs')
5 False
6 >>> print c.endswith('_f')
7 True
View Code

3、判断字符串中某个字符或者字符串出现的次数:

1 >>> c="as_as_ddd_Fge_f"
2 >>> print c.count('d')
3 3
4 >>> print c.count('_d')
5 1
6 >>> print c.count('_')
7 4
8 >>> print c.count('1')
9 0
View Code

4、判断某个字符串、列表、是否含有某个字符或字符串:

 1 >>> c="as_as_ddd_Fge_f"
 2 >>> print "a" in c
 3 True
 4 >>> print "as" in c
 5 True
 6 >>> print "1" in c
 7 False
 8 >>> c=['q','b','2']
 9 >>> print "q" in c
10 True
11 >>> print "qq" in c
12 False
13 >>> print "3" in c
14 False
15 >>> print "2" in c
16 True
View Code

 

posted @ 2018-04-11 16:58  David---wei  阅读(156)  评论(0编辑  收藏  举报