python字符串方法之查找

str.find()

str.rfind()

【作用:类似index,查找字符】

【英语:r=》right|右边,find=》寻找】

【说明:返回一个新的整数,查找到的位置,找不到出现-1,index找不到要报错】

In [190]:  "The stars are not afraid to appear like fireflies.".find("vition")#不存在返回-1
Out[190]: -1
In [192]:  "The stars are not afraid to appear like fireflies.".find("a")#从左边开始查找
Out[192]: 6
In [193]:  "The stars are not afraid to appear like fireflies.".rfind("a")#从右边开始查找
Out[193]: 32

str.count(sub[,start[,end]])

【作用:统计指定字符在字符串中出现的次数,可以根据起始和结束位置来统计】

【英语:count=>统计,case=》情况】

【说明:返回统计到的数量】

In [79]: "Man is a born child, his power is the power of growth. ".count("o")#从整个字符串中统计‘o’出现的次数
Out[79]: 5
In [80]: "Man is a born child, his power is the power of growth. ".count("o",0,11)#从位置0到11统计‘o’出现的次数
Out[80]: 1

str.index(sub[,start[,end]])

str.rindex(sub[,start[,end]])

【作用:从字符串左边开始查找,sub=》要查找的子字符串,start=》开始的位置,从0开始,end=>结束的位置】

【英语:index=>索引,r=>right|右边】

【说明:返回查找的字符串在源字符串的位置,找不到会报错】

In [63]: "What you are you do not see".index("e")#默认从左边开始查找,
Out[63]: 11
In [43]: "What you are you do not see".rindex("e")#从右边开始查找,
Out[43]: 26
In [45]: "What you are you do not see".rindex("W",1)#从位置1开始并且从右边开始查找
结果报错
In [46]: "What you are you do not see".rindex("d",0,20))#从位置0到20中并且从右边开始查找
Out[46]: 17
时间: 2024-08-29 00:13:54

python字符串方法之查找的相关文章

Python 字符串方法详解

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

python 字符串方法积累

字符串方法 str.strip([chars]) 返回字符串的副本,删除前导符和尾随符,chars参数为需要被删除的字符集,默认情况下删除空格,chars参数不是前缀或后缀,而是所有chars参数中的组合都被删除 例如: >>> 'www.example.com'.strip('cmowz.') 'example' >>> '   spacious   '.strip() 'spacious' str.join(iterable) 通过iterable将字符串串联起来并

Python字符串方法

方法 描述 string.capitalize( ) 返回首字母大写的字符串的副本 string.center(width[, fillchar]) 返回一个长度为max(len(string), width)且其中String的副本居中的字符串,两侧使用fillchar(默认为空字符)填充 string.count(sub[, start[, end]]) 计算子字符串sub的出现次数,可将搜索范围限制为string[start:end] string.decode([encoding[,er

几个Python字符串方法浅析

字符串提供了一系列的方法去实现复杂的文本处理任务.方法就是与特定的对象关联在一起的函数.方法调用同时进行了两次操作: 第一次:属性读取--具有object.attribute格式的表达式可以理解为"读取object对象的属性attribute的值": 第二次:函数调用表达式--具有函数(参数)格式的表达式意味着"调用函数代码,传递零或者更多用逗号隔开的参数对象,最后返回函数的返回值". 方法调用表达式对象,方法(参数)从左至右运行,也就是说Python首先读取对象方

python 字符串方法及列表,元组,字典(一)

字符串 str 注: 若想要保持单引号和双引号为字符串的一部分 1)单双引号交替使用, 2)使用转义字符\ 3)成对三个引号被存在变量里 二.字符串详细用法 字符串的单个取值例 p_1=”hello” 字符串元素定位置,通过索引 正序(→)  反序(←)开始值从0开始,反序从-1开始 取值方式  字符串名[索引值] 例print(p_1[-1])  取o ①字符串切片:字符串名[m:n:k]m:索引起始位置,n:索引结束位置+1,k:步长 默认值为1,取左不取右 例 1,print(p_1[2:

python字符串方法学习笔记

# 一.字符串大小写转换# 字符串首字符大写print("hello world".capitalize())# 将字符串变为标题print("hello WORLD".title())# 将字符串转为大写print("hello world".upper())# 把字符串转为小写print("HELLO WORLD".lower())# 翻转字符串中的大小写print("hello WORLD".swap

python字符串-方法

一.1. upper()作用:将字符串中字符转换为大写 In [17]: spam Out[17]: 'hello,world' In [18]: print(spam.upper()) HELLO,WORLD 2.lower()作用:将字符串中字符转换为小写 In [19]: spam = spam.upper() In [20]: spam Out[20]: 'HELLO,WORLD' In [21]: print(spam.lower()) hello,world 3.isupper()作

Python字符串解析方法汇总

Python字符串方法解析 1.capitalize 将首字母大写,其余的变成小写 print('text'.capitalize()) print('tExt'.capitalize()) 结果: Text Text 2.center  将字符串居中  ljust(从左到右填充),rjust(从右到左填充) a='test' print(a.center(20,'_')) print(a.rjust(20,'_')) 结果: ________test________ _____________

python字符串搜索

python字符串字串查找 find和index方法 更多0 python 字符串 python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法:查找子字符串,若找到返回从0开始的下标值,若找不到返回-1 info = 'abca'print info.find('a')##从下标0开始,查找在字符串里第一个出现的子串,返回结果:0 info = 'abca'print info.find('a',1)##从下标1开始,查