Friday, October 2, 2015

Python tutorial: Python Rindex String Method - Python Strings #68



To learn more about the Python Rindex String Method visit our website at http://learnpythontutorial.com/python-rindex-string-method/

Python Rindex String Method

In this Python tutorial, we will explore the Python rindex string method. The rindex string method is similar to the rfind string method the only difference is the rindex method returns a value error when a substring is not found in the string object and the rfind returns -1 when the substring is not located.



The rindex string method takes three arguments. The first argument is mandatory and that is the substring. The substring is the string we are about search for in the string object. If the substring is located in the string object, then we are returned the index position where the substring was located in the string object and if the substring is not located we are returned a value error. The next two arguments are voluntary. The second argument is the start index position and if do not provide a starting point then the rindex will default to the start of the string. The third argument is stopping index position and the default value is the end of the string object.



Python Rindex String Method Syntax

'String Object'.rindex('substring', start index, stop index)



'String Object' - The string object is where our rindex string method is going to search for a match.

.rindex() - The index string method searches through a string object to find the last occurrence in a string object and returns the index position of the last occurrence.

'substring' - The substring is what rindex is going try to match in the string object. If the substring is found in the object then Python will return the last occurrences index position. This argument is required and must be in string format.

start index - The start index is where the rindex will start its search. This is an optional argument and the default value is the beginning of the string.

stop index - The stop index is an optional argument. The stop index will indicate to the rindex string method where to stop the search for a match in the string object.  If the stop index is being used then there must be a start index position.

Examples Of The Python Rindex String Method

#Example 1

 'We are learning Python'.rindex('ing')

12



#Example 2

 'This is a string in Python'.rindex('n', 10)

25



#Example 3

 'This is a string in Python'.rindex('n', 10, 20)

18



#Example 4

 'This is a string in Python'.rindex('x', 10, 20)

Traceback (most recent call last):

  File "stdin", line 1, in module

ValueError: substring not found

Examples Explained



Example 1:



'We are learning Python'.rindex('ing') - In this example we create a string object and then call the rindex string method on the string object. We provide the rindex string method with one required argument. The substring argument is 'ing'.



12 - We are returned 12 this is the last occurrence in the string object.

Example 2:



'This is a string in Python'.rindex('n', 10) - In this example we create a new string object and call the rindex string method on the string object. We provide a substring argument of 'n' and a starting index position of 10.



25 - We are returned 25 this is position of the last occurrence in the string object

Example 3:



'This is a string in Python'.rindex('n', 10, 20) - We create a new string object and call the rindex string method on our string object. We provide a substring argument of 'n', a starting argument of 10 and ending argument of 20.



18 - We are returned 18 this is the last occurrence in the search position we provided.

Example 4:



'This is a string in Python'.rindex('x', 10, 20) - We create a new string and call our string method of rindex on our string object. We provide a substring argument of 'x', a starting argument of 10 and a ending argument of 20.



Traceback (most recent call last):



  File "stdin", line 1, in module



ValueError: substring not found - We are returned a value error since the 'x' substring was not located in the string object.



Conclusion



In this Python tutorial, we took a look at the Python rindex string method. If you have any questions about the rindex string method leave a comment below.

No comments:

Post a Comment