Python find()方法:检测字符串中是否包含某子串

1年前 (2024-04-27)
find() 方法用于检索字符串中是否包含目标字符串,如果包含,则返回次出现该字符串的索引;反之,则返回 -1。

find() 方法的语法格式如下:

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

此格式中各参数的含义如下:

  1. str:表示原字符串;

  2. sub:表示要检索的目标字符串;

  3. start:表示开始检索的起始位置。如果不指定,则默认从头开始检索;

  4. end:表示结束检索的结束位置。如果不指定,则默认一直检索到结尾。


【例 1】用 find() 方法检索 “c.biancheng网站站点" rel="nofollow" /> >>> str = "c.biancheng网站站点" rel="nofollow" /> >>> str = "c.biancheng网站站点" rel="nofollow" /> >>> str = "c.biancheng网站站点" rel="nofollow" /> >>> str = "c.biancheng网站站点" rel="nofollow" />