Open In App

Python String rfind() Method

Last Updated : 21 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1.

Example

s = "GeeksForGeeks"
print(s.rfind("Geeks"))

Output
8

Explanation

  • string “GeeksForGeeks” contains the substring “Geeks” twice.
  • rfind() method starts the search from the right side of the string, so it finds the last occurrence of “Geeks”, which starts at index 8 (the second occurrence).

Syntax of rfind() method

str.rfind(sub, start, end)

Parameters

  • sub: It’s the substring that needs to be searched in the given string. 
  • start: Starting position where the sub needs to be checked within the string. 
  • end: Ending position where suffix needs to be checked within the string. 

Return Type

  • Returns the right-most index of the substring if it is found in the given string; if not found, then it returns -1.

Note: If start and end indexes are not provided then, by default it takes 0 and length-1 as starting and ending indexes where ending indexes are not included in our search.

Examples of rfind() Method

1. Basic usages of Python String find() Method

word = 'geeks for geeks'

# Returns highest index of the substring
res = word.rfind('geeks')
print (res )

res = word.rfind('for')
print (res )

word = 'CatBatSatMatGate'

# Returns highest index of the substring
res = word.rfind('ate')
print(res)

Output
10
6
13

Explanation

  • Example 1: rfind(‘geeks’) in ‘geeks for geeks’ returns 12 (last occurrence of ‘geeks’).
  • Example 2: rfind(‘for’) in ‘geeks for geeks’ returns 5 (last occurrence of ‘for’).
  • Example 3: rfind(‘ate’) in ‘CatBatSatMatGate’ returns 12 (last occurrence of ‘ate’).

2. Using Python String rfind() Method with given start and end position inside String

If we pass the start and end parameters to the Python String rfind() Method, it will search for the substring in the portion of the String from its right side.

word = 'geeks for geeks'

# Substring is searched in 'eeks for geeks'
print(word.rfind('ge', 2))

# Substring is searched in 'eeks for geeks' 
print(word.rfind('geeks', 2))

# Substring is searched in 'eeks for geeks' 
print(word.rfind('geeks ', 2))

# Substring is searched in 's for g'
print(word.rfind('for ', 4, 11))

# finding substring using -ve indexing
print(word.rfind('geeks', -5))

Output
10
10
-1
6
10

Explanation

  • Example 1: rfind(‘ge’, 2) returns 2, as it finds ‘ge’ starting from index 2.
  • Example 2: rfind(‘geeks’, 2) returns 0, finding ‘geeks’ from index 2.
  • Example 3: rfind(‘geeks ‘, 2) returns -1, as it doesn’t find ‘geeks ‘ from index 2.
  • Example 4: rfind(‘for ‘, 4, 11) returns 6, finding ‘for ‘ within the range 4-11.
  • Example 5: rfind(‘geeks’, -5) returns 7, finding ‘geeks’ from index -5.

3. Practical Application

Here, we check one email address and the Top Level Domain (TLD) matching our necessary condition. Then we print, “Email matched” else “Email not matched”, followed by the TLD String. Even if this email address contains a “.com” substring, rfind() helped to extract the TLD string more efficiently.

email = 'userxyz.com@domain.xyz'
last_dot_pos = email.rfind('.', 1)
tld_string = email[last_dot_pos:]

if tld_string == ".com":
  print("Email matched")
else:
  print("Email not matched, tld:", tld_string)

Output
Email not matched, tld: .xyz

Explanation

  • rfind(‘.’, 1) searches for the last occurrence of ‘.’ in the string starting from index 1.
  • It finds the last dot ‘.’ at position 10 in ‘userxyz.com@domain.xyz’.
  • email[last_dot_pos:] extracts the TLD (Top-Level Domain) starting from the last dot: ‘.com’.
  • The code checks if the extracted TLD matches “.com”.
  • If it matches, “Email matched” is printed; otherwise, it prints the TLD.

Python String rfind() Method – FAQs

What does the rfind() method do in Python?

The rfind() method searches for the last occurrence of a substring in a string. It returns the index of the first character of the last occurrence of the substring, or -1 if the substring is not found.

How is rfind() different from find()?

  • find() searches for the first occurrence of a substring from left to right.
  • rfind() searches for the last occurrence of a substring from right to left.

What happens if the substring is not found?

If the substring is not found in the specified range, rfind() returns -1.

What is the output of rfind() when the substring is found at the first index?

If the substring is found at the first index (starting from the rightmost side), rfind() will return that index.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg