Python之字符串方法find( )
来源:互联网 发布:nba数据查询 编辑:程序博客网 时间:2024/04/29 17:47
不理解函数的含义会影响对代码的理解,举例如下。
e.g.模拟手机通讯录中的电话号码联想功能之基本思路。
代码:search = '188'
num_a = '1386-188-0006'
num_b = '1881-222-0006'
print(search + ' is at ' + str(num_a.find(search)) + ' to ' + str(num_a.find(search) + len(search)) + ' of num_a')
print(search + ' is at ' + str(num_b.find(search)) + ' to ' + str(num_b.find(search) + len(search)) + ' of num_b')
结果:188 is at 5 to 8 of num_a
188 is at 0 to 3 of num_b
Python中的find( )方法
用于检验字符串是否包含子字符串str,如果已指定beg和end范围,则检验将在制定范围内。
如果包含字符串,返回开始的索引值,否则返回-1。
语法:str.find(str,beg=0,end=len(string))
str——指定检索的字符串
beg——开始索引,默认为0
end——结束索引,默认为字符串的长度
num_a.find(search) 指在变量num_a中查找search值所对应的索引号;未有给出beg和end值即指默认。
而在num_a.find(search)外嵌套str( )则是避免print函数中的数值型数据和字符型数据合并时报错。
0 0
- Python之字符串方法find( )
- python字符串查找之 find和index方法
- python学习之——字符串查找 find 和 index方法
- python 学习之字符串方法
- python字符串字串查找 find和index方法
- Python学习笔记 - 5.字符串(find方法,格式操作符%)
- python字符串字串查找 find和index方法
- Python find()方法
- python语法之find
- python之find all
- ubuntu之find方法
- python文档字符串查找应用---find()
- 跟我一起学Python之九:字符串常用方法
- Python基础之字符串处理方法的总结
- python基础学习之字符串的常用方法
- python之自练小程序(字符串方法)
- python字符串格式化方法之%s和format函数
- Python全栈之路:string字符串常用方法
- OC 执行JS中的实例方法的那些坑
- 关于进程守护,拉起问题
- video标签-点击封面 播放视频,显示进度条
- DAY25:leetcode #53 Maximum Subarray
- 电子元器件分类
- Python之字符串方法find( )
- Struts2 中result type属性说明
- iOS性能调试
- 解决MFC不能进行多文件选择的问题
- 找出一串数字中的最大和子串
- ue4常用方法与技巧汇总
- CSS3选择器中容易误解的坑
- Fillder手机抓包
- Parajumpers Jacke your voice this what