.index() python
时间: 2024-05-25 18:18:37 浏览: 9
The .index() method in Python is a built-in function used to search for the first occurrence of a specified value in a list, tuple, or string. It returns the index of the first occurrence of the specified value in the given sequence. If the specified value is not found, it raises a ValueError exception.
The syntax of the .index() method is as follows:
sequence.index(value, start, end)
Here, sequence is the list, tuple, or string in which the value is to be searched, value is the element to be searched, start is the optional index from where the search must begin, and end is the optional index where the search must be stopped.
Example usage:
lst = [1, 2, 3, 4, 5]
print(lst.index(3)) # Output: 2
Here, the .index() method searches for the value 3 in the list lst and returns its index, which is 2.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)