第二十一篇:Python 入门-Python 字符串方法深入

第二十一篇:Python 字符串方法深入

1. 字符串查找与替换

find()、rfind() 方法查找子字符串位置

find() 方法用于在字符串中查找子字符串,并返回子字符串第一次出现的索引位置。如果子字符串不存在,则返回 -1。其语法为:str.find(sub[, start[, end]]),其中 sub 是要查找的子字符串,startend 是可选参数,用于指定查找的起始和结束位置(包括 start,不包括 end)。

text = "Python is a great programming language, Python is fun."
position = text.find
posted @ 2025-04-07 11:03  程序员勇哥  阅读(10)  评论(0)    收藏  举报  来源
/* 看板娘 */