YouTip LogoYouTip

Att String Rfind

Python rfind() Method | Simple Tutorial body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; line-height: 1.6; background-color: #f8f9fa; } .container { max-width: 1200px; margin: 0 auto; padding: 20px; } h1 { font-size: 2.5rem; color: #333; text-align: center; margin-bottom: 20px; } .nav-links { display: flex; justify-content: center; flex-wrap: wrap; gap: 10px; margin-bottom: 20px; } .nav-links a { color: #007bff; text-decoration: none; font-weight: 500; transition: color 0.3s ease; } .nav-links a:hover { color: #0056b3; } .tutorial-section { background-color: #fff; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); padding: 20px; margin-bottom: 20px; } code { font-family: 'Courier New', monospace; background-color: #f1f1f1; border: 1px solid #ddd; padding: 2px 6px; border-radius: 4px; font-size: 0.95rem; } pre { background-color: #f1f1f1; border: 1px solid #ddd; border-radius: 8px; padding: 15px; overflow-x: auto; margin-top: 10px; font-size: 0.95rem; } .footer { text-align: center; margin-top: 40px; color: #6c757d; font-size: 0.9rem; }

Python rfind() Method | Simple Tutorial

Python rfind() Method

The rfind() method returns the highest index in the string where the substring is found, or -1 if not found.

Syntax

str.rfind(sub[, start[, end]])

Parameters

  • sub: The substring to search for.
  • start (optional): The starting position of the search.
  • end (optional): The ending position of the search.

Return Value

Returns the highest index of the substring, or -1 if not found.

Example

text = "Hello, world! This is a test string."
index = text.rfind("test")
print(index)  # Output: 21

If the substring is not found:

text = "Hello, world! This is a test string."
index = text.rfind("xyz")
print(index)  # Output: -1

You can also specify a range to search within:

text = "Hello, world! This is a test string."
index = text.rfind("is", 10, 25)
print(index)  # Output: 16

Note: Unlike find(), rfind() searches from right to left and returns the last occurrence of the substring.

← Att String RindexAtt String Replace β†’