C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String rfind() MethodPython rfind() method finds a substring in in the string and returns the highest index. It means it returns the index of most righmost matched subtring of the string. It returns -1 if substring not found. Signature
rfind(sub[, start[, end]]) Parameters
sub : substring to be searched. start (optional) : starting index to start searching. end (optional) : end index where to search stopped. Return
It returns either index of substring or -1. Let's see some examples of rfind() method to understand it's functionality. Python String rfind() Method Example 1Let's have a simple example to implement the rfind() method. It returns highest index of the substring.
# Python rfind() method example
# Variable declaration
str = "Learn Java from TheDeveloperBlog"
# calling function
str2 = str.rfind("Java")
# displaying result
print(str2)
Output: 16 Python String rfind() Method Example 2One more example to understand the working of rfind() method.
# Python rfind() method example
# Variable declaration
str = "It is technical tutorial"
# calling function
str2 = str.rfind("t")
# displaying result
print(str2)
Output: 18 Python String rfind() Method Example 3This method takes other three parameters including two optional. Let's provide start and end index to the method.
# Python rfind() method example
# Variable declaration
str = "It is technical tutorial"
# calling function
str2 = str.rfind("t",5) # Only starting index is passed
# displaying result
print(str2)
str2 = str.rfind("t",5,10) # Start and End both indexes are passed
print(str2)
Output: 18 6
Next TopicPython Strings
|