python中字符查找函数 python字符串

python中怎么返回指定查找字符的位置Python编程中对字符串进行搜索查找python中字符查找函数 , 并返回字符位置python中字符查找函数,案例代码如下python中字符查找函数:
# multiple searches of a string for a substring
# using s.find(sub[ ,start[, end]])
#以下面test这段文本为例
text = 'MSKSASPKEPEQLRKLFIGGLSFETTDESLRSAHFESSSYGSAGRRF'
##查找上面文本中python中字符查找函数的SA字符串
search = 'SA'
start = 0
whileTrue:
index = text.find(search, start)
# if search string not found, find() returns -1
# search is complete, break out of the while loop
if index == -1:
break
print( "%s found at index %d"% (search, index) )
# move to next possible start position
start = index1
//运行结果:
#SA found at index 3
#SA found at index 31
#SA found at index 41
如何用Python来进行查询和替换一个文本字符串1、说明
可以使用find或者index来查询字符串,可以使用replace函数来替换字符串 。
【python中字符查找函数 python字符串】2、示例
1)查询
'abcdefg'.find('cde')
结果为2
'abcdefg'.find('acde')
结果为-1
'abcdefg'.index('cde')
结果为2
2)替换
'abcdefg'.replace('abc','cde')
结果为'cdedefg'
3、函数说明
1)find(...)
S.find(sub[, start[, end]]) - int
返回S中找到substring sub的最低索引,使得sub包含在S [start:end]中 。可选的参数start和end解释为切片表示法 。
失败时返回-1 。
2)index(...)
S.index(sub[, start[, end]]) - int
与find函数类似,但是当未找到子字符串时引发ValueError 。
3)replace(...)
S.replace(old, new[, count]) - str
返回S的所有出现的子串的副本旧换新 。如果可选参数计数为给定,只有第一个计数出现被替换 。
解释python中.find(" ")函数的作用:python中遇到不明白的地方,可以试试help
这里要查看find的作用 , 可以键入help(str.find) , 然后得到提示如下:
Help on method_descriptor:
find(...)
S.find(sub[, start[, end]]) - int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
解释要点大致如下:
find()方法检测字符串S中是否包含子字符串sub,如果指定start(开始) 和 end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值(如果包含多个字串 , 只返回最左边出现的索引值),查找失败返回-1 。以本题为例:
s="abcd1234"
s.find("cd"),在字符串s中查找字串"cd"第一次出现时s中的索引值,因为索引从0开始,所以结果为2,注意s中出现多次cd的情况,例如:
s="abcd1234cd"
s.find("cd")的结果依然是2,找不到时返回-1,比如:
s="1234"
s.find("cd")的结果为-1
python之字符串内置函数 1.字符串字母处理
2. 字符串填充
str.ljust(width, fillchar)、str.center(width, fillchar)、str.rjust(width, fillchar)
返回一个指定的宽度 width 「居左」/「居中」/「居右」的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充 。
3,字符串计数
str.count(sub, start, end)
#统计字符串里某个字符出现的次数 。可选参数为在字符串搜索的开始与结束位置 。
start, end遵循**“左闭右开”**原则 。
4. 字符串位置
str.endswith(suffix, start, end)和str.startswith(substr, beg, end)
#判断字符串是否以指定后缀结尾/开头,如果以指定后缀「结尾」/「开头」返回 True , 否则返回 False 。
5. 字符串查找
6. 字符串判断
7. 字符串拼接
str.join() #将序列中的元素以指定的字符连接生成一个新的字符串 。
s1 = "-" s2 = "" seq = ("r", "u", "n", "o", "o", "b")
# 字符串序列 print (s1.join( seq )) print (s2.join( seq )) r-u-n-o-o-b runoob
8. 统计字符串长度
str.len() #返回对象(字符、列表、元组等)长度或项目个数 。
9. 去除字符两侧空格
str.lstrip()、str.rstrip()、str.strip() #截掉字符串「左边」/「右边」/「左右」两侧的空格或指定字符 。
str0 = ' Hello World!' str0.lstrip() 'Hello World!' str1 = 'aaaa Hello World!' str1.lstrip('a') ' Hello World!'
10. str.maketrans(intab, outtab)和str.translate(table)
str.maketrans()创建字符映射的转换表
str.maketrans()根据参数table给出的表转换字符串的字符 。
str.maketrans()传入的也可以是字典
tab = {'e': '3', 'o': '4'} trantab = str.maketrans(tab) str0.translate(trantab) 'H3ll4 W4rld!'
11. 字符串替换
str.replace(old, new, max)
12. 字符分割
str.split(str, num)
13. 字符填充
str.zfill(width)
返回指定长度的字符串,原字符串右对齐 , 前面填充0 。
如何用Python语言实现在一个文件中查找特定的字符串?用正则表达式
s='hello world'
import re
re.search('wor',s)
_sre.SRE_Match object; span=(6, 9), match='wor'
Python API快餐教程(1) - 字符串查找API字符串是7种序列类型中的一种 。
除了序列的操作函数,比如len()来求字符串长度之外,Python还为字符串提供丰富到可以写个编辑器的API.
首先 , 下面的查找API都是为了查找位置 , 或者查一共有几次这样的操作 。
如果只是想判断一个字符串是不是另一个字符串的子串的话 , 使用序列的in运算符就可以了 。
例:
原型:str.count(sub[, start[, end]])
字符串的count函数可以数出来有多少次匹配,我们看个例子 , 有5个ha和3个hei
输出为5和2.
haha只能匹配两次 。
再加上扩展参数:
find函数的行为是,如果能找到,则返回在序列中的坐标,如果找不到,则返回-1. rfind是从右向左查找 。我们来看例子:
输出值为0和6.
找不到的例子:
输出值都是-1.
完整形式:
index和rindex的功能与find和rfind基本上一致 , 除了在找不到时会抛出ValueError异常而不是返回-1.
例:
所以我们需要加try...except语句来处理之:
有时候,我们希望做从头匹配或者匹配尾部 。这时候就要用到startswith函数和endswith函数 。例:
这两个返回值均为True.
如果需要更复杂的匹配,还是需要正则表达式 。与Java等语言不同,Python中的正则表达式有专门的模块 , 字符串的API不负责这个事情 。
python中字符查找函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python字符串、python中字符查找函数的信息别忘了在本站进行查找喔 。

    推荐阅读