程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python index() method

編輯:Python

List of articles

  • Python index() Method : Detects whether a string contains a substring


Python index() Method : Detects whether a string contains a substring

Same as find() The method is similar to ,index() Method can also be used to retrieve whether the specified string is included , The difference is , When the specified string does not exist ,index() The method throws an exception .

index() The syntax of the method is as follows :

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

The meanings of parameters in this format are :

  • str: Represents the original string ;
  • sub: Represents the substring to retrieve ;
  • start: Indicates the starting position of the search , If you don't specify , Search from scratch by default ;
  • end: Indicates the end of the search , If you don't specify , The default is to retrieve all the way to the end .

【 example 1】 use index() Method retrieval “c.biancheng.net” First time in “.” Location index of .

>>> str = "c.biancheng.net"
>>> str.index('.')
1

【 example 2】 When retrieval fails ,index() It throws an exception .

>>> str = "c.biancheng.net"
>>> str.index('z')
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
str.index('z')
ValueError: substring not found

Same as find() and rfind() equally , String variables also have rindex() Method , Its function and index() The method is similar to , The difference is that it searches from the right , for example :

>>> str = "c.biancheng.net"
>>> str.rindex('.')
11

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved