Python find()方法

Python find()方法


描述

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

语法

find()方法语法:

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

参数

  • str -- 指定检索的字符串
  • beg -- 开始索引,默认为0。
  • end -- 结束索引,默认为字符串的长度。

返回值

如果包含子字符串返回开始的索引值,否则返回-1。

实例

以下实例展示了find()方法的实例:

实例(Python 2.0+)

#!/usr/bin/python

str1 = "this is string example....wow!!!";

str2 = "exam"; print str1.find(str2);

print str1.find(str2, 10);

print str1.find(str2, 40);

以上实例输出结果如下:

15
15
-1

原文地址:https://www.cnblogs.com/DJRemix/p/11758747.html

时间: 2024-08-03 01:17:18

Python find()方法的相关文章

Python list方法总结

1.向列表的尾部添加一个新的元素 append(...) L.append(object) -- append object to end >>> a = ['sam',24,'shaw'] >>> a.append('35') >>> a ['sam', 24, 'shaw', '35'] 2.查找list中有多少个value count(...) L.count(value) -> integer -- returnnumber of occ

Python capitalize()方法

Python capitalize()方法 capitalize()方法返回字符串的一个副本,只有它的第一个字母大写.对于8位的字符串,这个方法与语言环境相关. 语法 以下是capitalize()方法的语法: str.capitalize() 参数 NA 返回值 此方法返回的字符串只有它的第一个字符大写的副本. 例子 下面的示例演示了capitalize方法的使用. #!/usr/bin/python str = "this is string example....wow!!!";

Python字典方法copy()和deepcopy()的区别

1 from copy import deepcopy # import deepcopy模块 2 d = {} 3 d['name'] = ['black', 'guts'] # d = {'name': ['black', 'guts']} 4 c = d.copy() # c = {'name': ['black', 'guts']} 5 dc = deepcopy(d) # dc = {'name': ['black', 'guts']} 6 d['name'].append('whit

Python strip()方法

描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 语法 strip()方法语法: str.strip([chars]); 参数 chars -- 移除字符串头尾指定的字符. 返回值 返回移除字符串头尾指定的字符生成的新字符串. 实例 以下实例展示了strip()函数的使用方法: #!/usr/bin/python str = "0000000this is string example....wow!!!0000000"; print str.str

Python startswith()方法

描述 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内检查. 语法 startswith()方法语法: str.startswith(str, beg=0,end=len(string)); 参数 str -- 检测的字符串. strbeg -- 可选参数用于设置字符串检测的起始位置. strend -- 可选参数用于设置字符串检测的结束位置. 返回值 如果检测到

Python 字符串方法详解

Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. 在编程中,几乎90% 以上的代码都是关于整数或字符串操作,所以与整数一样,Python 的字符串实现也使用了许多拿优化技术,使得字符串的性能达到极致.与 C++ 标准库(STL)中的 std::string 不同,python 字符串集合了许多字符串相关的算法,以方法成员的方式提供接口,使用起来非常方便. 字符

Python replace()方法

描述 Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次. 语法 replace()方法语法: str.replace(old, new[, max]) 参数 old -- 将被替换的子字符串. new -- 新字符串,用于替换old子字符串. max -- 可选字符串, 替换不超过 max 次 返回值 返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个

Python count()方法

描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. 语法 count()方法语法: 1 str.count(sub, start= 0,end=len(string)) 参数 sub -- 搜索的子字符串 start -- 字符串开始搜索的位置.默认为第一个字符,第一个字符索引值为0. end -- 字符串中结束搜索的位置.字符中第一个字符的索引为 0.默认为字符串的最后一个位置. 返回值 该方法返回子字符串在字符串中出现的次数.

Python strip()方法介绍

描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 语法 strip()方法语法: str.strip([chars]); 参数 chars -- 移除字符串头尾指定的字符. 返回值 返回移除字符串头尾指定的字符生成的新字符串. 特别注意:移除的字符串的头尾指定字符,不如不是头尾字符不会移除 例1: >>> a = '     123    '>>> a.strip()'123' 例2: >>> a = '  123

Python str方法总结

1.返回第一个字母大写 S.capitalize(...) S.capitalize() -> string >>>a = 'shaw' >>> b = a.capitalize() >>> print b Shaw 2.按指定长度填充特定字符 center(...) S.center(width[, fillchar]) -> string >>> a = 'linux' >>> print a.cen