python字符串的index和find的区别详解


Posted in Python onJune 20, 2020

1.find函数

find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果指定范围内如果包含指定索引值,返回的是索引值在字符串中的起始位置。如果不包含索引值,返回-1。

string='abcde'
x=string.find('a')
y=string.find('bc')
z=string.find('f')
print(x)
print(y)
print(z)
#运行结果
0
1
-1

2.index函数

index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

string='abcde'
x=string.index('a')
y=string.index('bc')
#z=string.index('f')
print(x)
print(y)
#print(z)
0
1
ValueError: substring not found

3.join 函数

Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

lis=['a','b','c','d','e']
string='abcde'
tup=('a','b','c','d','e')
print(''.join(lis))
print(' '.join(string))
print('$'.join(tup))
#运行结果
abcde
a b c d e
a$b$c$d$e

注意序列里的元素必须是字符串,不能是数字

4.split函数

split() 通过指定分隔符对字符串进行切片,如果第二个参数 num 有指定值,则分割为 num+1 个子字符串。

str.split(str="", num=string.count(str))

string='this is an interesting story!'
a=string.split()
b=string.split(' ',2)
c=string.split('s')
d=string.split(',')
print(a)
print(b)
print(c)
print(d)
#运行结果
['this', 'is', 'an', 'interesting', 'story!']
['this', 'is', 'an interesting story!']
['thi', ' i', ' an intere', 'ting ', 'tory!']
['this is an interesting story!']

5.strip函数

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

string='**this is an ***interesting story!***'
a=string.strip('*')
b=string.lstrip('*')
c=string.rstrip('*')
print(string)
print(a)
print(b)
print(c)
#运行结果
**this is an ***interesting story!***
this is an ***interesting story!
this is an ***interesting story!***
**this is an ***interesting story!

lstrip和rstrip分别去掉左边和右边的指定字符。

到此这篇关于python字符串的index和find的区别详解的文章就介绍到这了,更多相关python字符串的index和find的区别内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python中的面向对象编程详解(下)
Apr 13 Python
一些Python中的二维数组的操作方法
May 02 Python
python通过索引遍历列表的方法
May 04 Python
python通过imaplib模块读取gmail里邮件的方法
May 08 Python
Python排序搜索基本算法之插入排序实例分析
Dec 11 Python
python实现xlsx文件分析详解
Jan 02 Python
详解python使用pip安装第三方库(工具包)速度慢、超时、失败的解决方案
Dec 02 Python
在python中按照特定顺序访问字典的方法详解
Dec 14 Python
Python工程师必考的6个经典面试题
Jun 28 Python
Python Pygame实现俄罗斯方块
Feb 19 Python
Python使用Kubernetes API访问集群
May 30 Python
python ConfigParser库的使用及遇到的坑
Feb 12 Python
Django Admin 上传文件到七牛云的示例代码
Jun 20 #Python
什么是python的函数体
Jun 19 #Python
浅谈Python中的生成器和迭代器
Jun 19 #Python
python中有帮助函数吗
Jun 19 #Python
python中导入 train_test_split提示错误的解决
Jun 19 #Python
python中get和post有什么区别
Jun 19 #Python
python中setuptools的作用是什么
Jun 19 #Python
You might like
php自动获取关键字的方法
2015/01/06 PHP
PHP中SSO Cookie登录分析和实现
2015/11/06 PHP
区分JS中的undefined,null,"",0和false
2007/03/08 Javascript
通过javascript获取iframe里的值示例代码
2013/06/24 Javascript
js 得到文件后缀(通过正则实现)
2013/07/08 Javascript
查找iframe里元素的方法可传参
2013/09/11 Javascript
JS+CSS 制作的超级简单的下拉菜单附图
2013/11/22 Javascript
js模仿hover的具体实现代码
2013/12/30 Javascript
纯JavaScript代码实现文本比较工具
2016/02/17 Javascript
原生JS实现网络彩票投注效果
2016/09/25 Javascript
整理一下常见的IE错误
2016/11/18 Javascript
BootStrapValidator初使用教程详解
2017/02/10 Javascript
jQuery判断网页是否已经滚动到浏览器底部的实现方法
2017/10/27 jQuery
vue利用axios来完成数据的交互
2018/03/23 Javascript
websocket4.0+typescript 实现热更新的方法
2019/08/14 Javascript
如何正确理解vue中的key详解
2019/11/02 Javascript
js 闭包深入理解与实例分析
2020/03/19 Javascript
vue scroll滚动判断的实现(是否滚动到底部、滚动方向、滚动节流、获取滚动区域dom元素)
2020/06/11 Javascript
Python3实现Web网页图片下载
2016/01/28 Python
Python使用Matplotlib实现雨点图动画效果的方法
2017/12/23 Python
python类的方法属性与方法属性的动态绑定代码详解
2017/12/27 Python
如何基于Python批量下载音乐
2019/11/11 Python
django使用xadmin的全局配置详解
2019/11/15 Python
Python计算矩阵的和积的实例详解
2020/09/10 Python
python中温度单位转换的实例方法
2020/12/27 Python
python爬取微博评论的实例讲解
2021/01/15 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
2021/01/15 Python
WiFi云数码相框:Nixplay
2018/07/05 全球购物
Abbacino官网:包、钱包和女士配饰
2019/04/15 全球购物
化妆品店促销方案
2014/02/24 职场文书
农村党员一句话承诺
2014/05/30 职场文书
生产操作工岗位职责
2014/09/16 职场文书
2014党员干部四风问题对照检查材料思想汇报
2014/09/24 职场文书
2014年电厂工作总结
2014/12/04 职场文书
导游词之太湖
2019/10/08 职场文书
vue组件的路由高亮问题解决方法
2021/05/11 Vue.js