python search a string in another string get last result 1

python search a string in another string get last result

# Where in the text is the last occurrence of the string "casa"?
txt = "Mi casa, su casa."

x = txt.rfind("casa")

print(x)

Here is what the above code is Doing:
1. We create a variable called txt, and assign it a string.
2. We use the rfind() method to find the last occurrence of the word “casa”.
3. The rfind() method returns the index of the last occurrence of the specified value.
4. We print the result.

Similar Posts