Python find() 方法
2023-09-27 14:21:28 时间
正文
描述
Python find() 方法从字符串中找出某个子字符串第一个匹配项的索引位置,该方法与 index() 方法一样,只不过如果子字符串不在字符串中不会报异常,而是返回-1。
语法
find() 方法语法:
S.find(sub[,start=0[,end=len(S)]])
参数
- sub -- 指定检索的子字符串
- S -- 父字符串
- start -- 可选参数,开始索引,默认为0。(可单独指定)
- end -- 可选参数,结束索引,默认为字符串的长度。(不能单独指定)
返回值
以下实例展示了 find() 方法的实例:
#!/usr/bin/python3 S1 = "Runoob example....wow!!!" S2 = "exam"; print (S1.find(S2)) print (S1.find(S2, 5)) print (S1.find(S2, 10))
以上实例输出结果如下:
7 7 -1
再看一个实例:
>>>info = 'abca' >>> print(info.find('a')) # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0 0 >>> print(info.find('a', 1)) # 从下标1开始,查找在字符串里第一个出现的子串:返回结果3 3 >>> print(info.find('3')) # 查找不到返回-1 -1 >>>
相关文章
- Python 使用 Scrapy 发送 post 请求的坑
- python __getitem__()方法理解 python之使用魔术方法__getitem__和__len__
- Python 对数组元素操作的三种方法
- python中eval()和json.loads()格式化字符串效率
- 不用pip官方源安装Python扩展库的方法(使用国内的镜像源)
- 【Python】字符串不足位数补零/补空格
- Python第三方库安装方法-转载
- python学习之报错:ModuleNotFoundError: No module named ‘pandas‘解决方法
- python学习之word文档转换成pdf文档
- 基于Python(Tkinter)实现(图形界面)小说阅读器【100010450】
- python pip 安装包下载过慢的解决方法
- Python个人主页项目-1.项目分析与产品设计
- python语言绘图:绘制贝叶斯方法中最大后验密度(Highest Posterior Density, HPD)区间图的近似计算
- 【Python实战02】共享Python代码到PyPI社区
- 华为OD机试 - 双十一(Python) | 机试题+算法思路+考点+代码解析 【2023】
- Python!Are you kidding me?
- python字符串find方法,python findall函数用法
- python中两个不同shape的数组间运算规则
- 数学思想方法-python计算战(8)-机器视觉-二值化
- Python strip()方法,split()方法和replace()的区别(易混淆)
- python贡献度分析20/80定律
- 【Python-opencv识别二维码问题解决方法】:FileNotFoundError: Could not find module ‘libiconv.dll‘
- 【图像处理】——Python实现two_pass方法来进行连通域的提取
- 【Python】pycharm opencv没有智能提示开启
- (原)python中import caffe提示no module named google.protobuf.internal