python字符串相关操作

本文介绍了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

 

转载于:https://www.cnblogs.com/st12345/p/8796810.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值