
1、len计算字符串的字符数量。
一个汉字或一个字母算一个字符。
name = '我是aa\n' # \n作为换行符,算一个字符 num = len(name) print(num) 输出:5
2、find检索子串的索引/下标位置,从0开始检索相应的索引。
找不到时返回-1。
# a.find() # 从0开始找对应索引;如果有多个,则找从左数的第一个
msg = 'The day is a sunny day'
result1 = msg.find('d')
print(result1)
result2 = msg.find('d',5,-1) # 从第五个索引开始到最后一个索引之间,'d'出现的第一个索引
print(result2)
result3 = msg.find('d',5,9) # 从第五个索引开始到第九个索引之间(包含5不包含9),'d'出现的第一个索引
print(result3)
输出:
4
19
-1
3、rfind功能与find相同,不同之处在于从右边开始寻找相应的索引。
# a.rfind() # 从右边开始找对应索引
msg = 'The day is a sunny day'
result1 = msg.rfind('d')
result2 = msg.rfind('da') # 找多个字符,会返回找到的'd'的索引
print(result1)
print(result2)
输出:
19
19
4、index/rindex返回所需值的索引。
msg = 'The day is a sunny day'
result = msg.index('dd')
print(result)
输出:
Traceback (most recent call last):
File "xxx.py", line 43, in
result = msg.index('dd')
ValueError: substring not found
以上就是python查找计算函数的整理,希望对大家有所帮助。更多Python学习指路:创新互联Python教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
分享名称:创新互联Python教程:python查找计算函数的整理
文章源于:http://www.csdahua.cn/qtweb/news44/444944.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网