
python 提取两个字符串之间的内容
find()方法,检索指定的字符串,如果存在返回首次出现该字符串的索引,如果不存在返回-1;(从左往右)rfind()方法,如果存在则返回最后一个索引,或者不存在返回-1;(从右往左)可选限制搜索为:string[beg:end].
·
rfind()方法如果找到子字符串str则返回最后一个索引,或者如果没有这样的索引存在返回-1,可选限制搜索为:string[beg:end].
参数
-
str -- 此选项指定要搜索的字符串
-
beg -- 这是起始索引,默认是0
-
end -- 这是结束索引,默认情况下其等于该字符串的长度
返回值
此方法如果找到则返回最后一个索引,否则返回-1。
#!/usr/bin/python3
str1 = "{this is really a string example....wow!!!"
print(strs[strs.rfind('{'):strs.rfind("!!!")+1].
结果:this is really a string example....wow!!!
更多推荐
所有评论(0)