第二十一篇:Python 字符串方法深入
1. 字符串查找与替换
find()、rfind() 方法查找子字符串位置
find()
方法用于在字符串中查找子字符串,并返回子字符串第一次出现的索引位置。如果子字符串不存在,则返回 -1
。其语法为:str.find(sub[, start[, end]])
,其中 sub
是要查找的子字符串,start
和 end
是可选参数,用于指定查找的起始和结束位置(包括 start
,不包括 end
)。
text = "Python is a great programming language, Python is fun."
position = text.find("Python"