Python中的rfind()方法使用详解

yipeiwu_com5年前Python基础

 rfind()方法返回所在子str 被找到的最后一个索引,或者-1,如果没有这样的索引不存在,可选择限制搜索字符串string[beg:end].
语法

以下是rfind()方法的语法:

str.rfind(str, beg=0 end=len(string))

参数

  •     str -- 此选项指定要搜索的字符串
  •     beg -- 这是开始索引,默认情况下为 0
  •     end -- 这是结束索引,默认情况下它等于该字符串的长度

返回值

此方法如果找到返回最后一个索引,否则返回-1
例子

下面的例子显示了rfind()方法的使用

#!/usr/bin/python

str = "this is really a string example....wow!!!";
str = "is";

print str.rfind(str);
print str.rfind(str, 0, 10);
print str.rfind(str, 10, 0);

print str.find(str);
print str.find(str, 0, 10);
print str.find(str, 10, 0);

当我们运行上面的程序,它会产生以下结果:

5
5
-1
2
2
-1

相关文章

Python函数中定义参数的四种方式

Python中函数参数的定义主要有四种方式: 1. F(arg1,arg2,…) 这是最常见的定义方式,一个函数可以定义任意个参数,每个参数间用逗号分割,用这种方式定义的函数在调用的的时...

python实现上传文件到linux指定目录的方法

python实现上传文件到linux指定目录的方法

今天接到一个小需求,就是想在windows环境下,上传压缩文件到linux指定的目录位置并且解压出来,然后我想了一下,这个可以用python试试写下。 环境: 1.linux操作系统一台...

使用python实现knn算法

使用python实现knn算法

本文实例为大家分享了python实现knn算法的具体代码,供大家参考,具体内容如下 knn算法描述 对需要分类的点依次执行以下操作: 1.计算已知类别数据集中每个点与该点之间的距离 2....

python使用内存zipfile对象在内存中打包文件示例

复制代码 代码如下:import zipfileimport StringIO class InMemoryZip(object):    def __in...

Python字符串的常见操作实例小结

本文实例讲述了Python字符串的常见操作。分享给大家供大家参考,具体如下: 如果我们想要查看以下功能:help(mystr .find) 1.find 例: mystr="hell...