博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python字符串内建函数str.index()和str.rindex()
阅读量:4171 次
发布时间:2019-05-26

本文共 1050 字,大约阅读时间需要 3 分钟。

本文介绍python字符串内建函数str.index( )和str.rindex( )的使用。

1》首先,通过help( str.index)函数获取帮助:

>>> help(str.index)

Help on method_descriptor:
index(...)
    S.index(sub [,start [,end]]) -> int
    

    Like S.find() but raise ValueError when the substring is not found.

str.index( )函数的使用,举例如下:

>>> s='love python!'

>>> s.index('ove') # 默认的查找区间是  [0,len(s))
1
>>> s.index('ove',2) # 只给出了查找起点是2,则对应的查找区间是 [2,len(s))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s.index('ove',1) # 只给出了查找起点是1,则对应的查找区间是 [1,len(s))
1
>>> s.index('ove',1,4) # 指定查找区间是 [1,4)
1
>>> s.index('ove',1,3) # 指定查找区间是 [1,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

2》通过help(str.rindex)获取帮助信息:

>>> help(str.rindex)

Help on method_descriptor:
rindex(...)
    S.rindex(sub [,start [,end]]) -> int
    
    Like S.rfind() but raise ValueError when the substring is not found.

>>> s='love python love python!'

>>> s.index('python') #返回左边第一个子串'python'的下标
5
>>> s.rindex('python')#返回右边第一个子串'python'的下标
17

(完)

转载地址:http://wdyai.baihongyu.com/

你可能感兴趣的文章
poj 1860 拓扑。。
查看>>
poj 2553 The Bottom of a Graph 未完
查看>>
inux下如何统计一个目录下的文件个数以及代码总行数(转)
查看>>
Linux下 虚拟机Bochs的使用
查看>>
glib-读取配置文件
查看>>
SonarQube 静态代码检查的安装
查看>>
嵌入式Linux驱动开发的知识图谱
查看>>
Algorithm 4th environment setup
查看>>
Linux设备驱动开发基础之互斥与同步基础
查看>>
Linux驱动开发之内存管理基础
查看>>
用gitlabCI快速搭建一个GitServer与CI
查看>>
SPI Nor Flash
查看>>
ARM Linux BenchMark
查看>>
完整精确导入Kernel与Uboot参与编译了的代码到Source Insight,Understand, SlickEdit
查看>>
Freescale IMX6 Android (5): APP通过JNI控制LED
查看>>
PPT分享: Linux启动流程 关于initrd与initramfs的区分及其发展历程
查看>>
Freescale IMX6 Android (7): Android启动动画死循环 Home界面不出来与pid XXX exit 可能的原因汇总
查看>>
Yocto i.MX6 (TQIMX6) (01) : 3.14.28内核的适配
查看>>
Yocto tips (6): Yocto中如何共享已经下载的文件
查看>>
Yocto tips (1): Yocto 编译后文件放在了哪里 输出文件位置
查看>>