Friday, October 2, 2015

Python Tutorial: Python Rfind String Method - Python Strings #67





For more information on the Python Rfind String Method visit our website at http://learnpythontutorial.com/python-rfind-string-method/

Python Rfind String Method

In this Python tutorial, we will learn how to use the Python rfind string method. The rfind string method will find the highest occurrence of the substring argument which is the last match in the string object. The rfind string method searches a string from the left to right to find the substring argument and return an index position of the match. The rfind string method takes three arguments. The first argument is our substring which is what Python is about search for in the string object. The second argument which is optional and indicates the starting position for the search and default position is the start of the string object. The third argument which is also optional and indicates the ending position of the search and the default value is the end of the string. If you want to declare an ending position you must declare a starting position argument. If Python does not find a match for the substring in the string object, then Python will return a -1.



Python Rfind String Method Syntax

'String Object'.rfind('substring', start, stop)



'String Object' - The string object is the string that we be searched using the rfind string method.

.rfind() - The rfind string method will search the string object for our substring and return the highest index position match.

'substring' - The substring is the string that will be searched for in the string object. If the substring is found in the string object then Python will return a index position of the last match. The substring search is case sensitive.

start - The start position is where the rfind string method will start the search for the substring. If no search position is given then the start position will default to the beginning of the string object.

stop - The stop position is where the rfind string method will stop the search for the substring. Remember when slicing the ending index ends one position before the indicated position.

Examples Of The Python Rfind String Method

#Example 1

 'This is our string'.rfind('st')

12



#Example 2

 'This is our string'.rfind('is', 4)

5



#Example 3

 'This is our string'.rfind('i')

15



#Example 4

 'This is our string'.rfind('i', 4, 7)

5



#Example 5

 'This is our string'.rfind('t', 4, 7)

-1

Examples Explained



Example 1:



'This is our string'.rfind('st') - We create a string object and call the rfind string method on our object. Our string method has an substring argument which the rfind method will search for in our string object.



12 - We are returned 12 which is the index position and is our last occurrence of our substring ('st')



Example 2:



'This is our string'.rfind('is', 4) - We create a new string object and call the rfind string method on the string object. Our method takes an argument of ('is') for the substring and 4 for the index position to start the search.



5 - We are returned 5 since the last occurrence in our string object is at the 5 index position.



Example 3:



'This is our string'.rfind('i') - We create a new string object and then we call the rfind string method on our string object. We provide one argument for the substring of ('i').



15 - We are returned 15 since the rfind string method finds the last 'i' in the string object.

Example 4:



'This is our string'.rfind('i', 4, 7) - We create a new string object and call the rfind string method on our string object. We provide a substring argument of 'i' and we also provide a start argument of 4 and stop argument of 7.



5 - We are returned 5 since the rfind string method on searches between the 5 index position and the 7 index position but remember the stopping point is actually the 6 index position.

Example 5:



'This is our string'.rfind('t', 4, 7) - We create a new string object and call the rfind string method on our string object. We provide an argument of 't' for the substring which we be searched for in the string object. We then provide a starting index position of 4 and an ending position of 7 which is actually 6.



-1 - We are returned a -1 since Python did not find a 't' between the index positions of 4 and 7.

Conclusion



In this Python tutorial we looked at the Python Rfind String Method. If you have any questions about the rfind string method leave a comment below and we will do our best to help you out.

No comments:

Post a Comment