index()
str = "hello world" print(("l",5,11)) # Results: 9 print(("l",3)) # The result is: 3 print(("l")) # Results are: 2 print(("c")) #in the end: report an error!!!
Syntax: variable.index(specified character, start subscript, end subscript)
The start subscript (i.e., 5) and the end subscript (i.e., 11) can be left out, and the default is to lookup from beginning to end.
When there is a start subscript and an end subscript, the search is for the interval between the start and the end of the string.
Returns the subscript position of the specified character (i.e., l) in a string (i.e., str) when it is found.
An error is reported if the specified character is not found in the string!
substring not found
rindex()
str = "hello world" print(("l",5,11)) # Results: 9 print(("l",3)) # The result is: 9 print(("l")) # Results are: 9 print(("c")) #in the end: report an error!!!
Syntax: variable.rindex(specified character, start subscript, end subscript)
Finding the specified character from the right (i.e., l) returns the subscript of the specified character in the string (i.e., str).
The start subscript (i.e., 5) and end subscript (i.e., 11) can be left out, and the entire string is looked up by default.
When there is a start subscript and an end subscript, the search is for the interval between the start and the end of the string.
Returns the subscript position of the specified character (i.e., l) in a string (i.e., str) when it is found.
An error is reported if the specified character is not found in the string!
substring not found
Knowledge Point Expansion:
Using the rindex() method to manipulate strings in Python
Want to understand the use of Python operation string of rindex () method of the relevant content? In this article for you to carefully explain Python's rindex () method of the relevant knowledge and some Code examples, welcome to read and corrections, we first draw the key points: Python, the following everyone together to learn it.
The rindex() method returns the last index at which the substr in which it is located was found, optionally restricting the search to the string string[beg:end] If no such index exists, an exception is thrown.
grammatical
The following is the syntax of the rindex() method:
(str, beg=0 end=len(string))
parameters
- str -- This option specifies the string to be searched.
- beg -- this is the start index, by default it is 0
- len -- This is the last index, by default it is equal to the length of the string.
return value
This method returns the last index if found, otherwise it raises an exception if str is not found.
(for) instance
The following example shows the use of the rindex() method.
#!/usr/bin/python str1 = "this is string example....wow!!!"; str2 = "is"; print (str2); print (str2);
When we run the above program, it produces the following results:
5
2
This article on the use of python index() and rindex() method is introduced to this article, more related to the use of python index() and rindex() method content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!