❮ String Methods
Example
Where in the text is the last occurrence of the string "casa"?:
txt = "Mi casa, su casa."
x = txt.rfind["casa"]
print[x]
Try it Yourself »
Definition and Usage
The
rfind[]
method finds the last occurrence of the specified value.
The rfind[]
method returns -1 if the value is not found.
The rfind[]
method is almost the same as the rindex[]
method. See example below.
Syntax
string.rfind[value, start, end]
Parameter Values
value | Required. The value to search for |
start | Optional. Where to start the search. Default is 0 |
end | Optional. Where to end the search. Default is to the end of the string |
More Examples
Example
Where in the text is the last occurrence of the letter "e"?:
txt = "Hello, welcome to my world."
x = txt.rfind["e"]
print[x]
Try it Yourself »
Example
Where in the text is the last occurrence of the letter "e" when you only search between position 5 and 10?:
txt = "Hello, welcome to my world."
x = txt.rfind["e", 5, 10]
print[x]
Try it Yourself »
Example
If the value is not found, the rfind[] method returns -1, but the rindex[] method will raise an exception:
txt = "Hello, welcome to my world."
print[txt.rfind["q"]]
print[txt.rindex["q"]]
Try it Yourself »
❮ String Methods
In this tutorial, we will learn about the Python index[] method with the help of examples.
The index[]
method returns the index of a substring inside the string [if found]. If the substring is not found, it raises an exception.
Example
text = 'Python is fun'
# find the index of is
result = text.index['is']
print[result]
# Output: 7
index[] Syntax
It's syntax is:
str.index[sub[, start[, end]] ]
index[] Parameters
The index[]
method takes
three parameters:
- sub - substring to be searched in the string str.
- start and end[optional] - substring is searched within str[start:end]
index[] Return Value
- If substring exists inside the string, it returns the lowest index in the string where substring is found.
- If substring doesn't exist inside the string, it raises a ValueError exception.
The index[]
method is similar to the find[] method for strings.
The only difference is that find[] method returns -1 if the substring is not found, whereas index[]
throws an exception.
Example 1: index[] With Substring argument Only
sentence = 'Python programming is fun.'
result = sentence.index['is fun']
print["Substring 'is fun':", result]
result = sentence.index['Java']
print["Substring 'Java':", result]
Output
Substring 'is fun': 19 Traceback [most recent call last]: File "", line 6, in result = sentence.index['Java'] ValueError: substring not found
Note: Index in Python starts from 0 and not 1. So the occurrence is 19 and not 20.
Example 2: index[] With start and end Arguments
sentence = 'Python programming is fun.'
# Substring is searched in 'gramming is fun.'
print[sentence.index['ing', 10]]
# Substring is searched in 'gramming is '
print[sentence.index['g is', 10, -4]]
# Substring is searched in 'programming'
print[sentence.index['fun', 7, 18]]
Output
15 17 Traceback [most recent call last]: File "", line 10, in print[quote.index['fun', 7, 18]] ValueError: substring not found
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Sometimes, while working with strings, we need to find if a substring exists in the string. This problem is quite common and its solution has been discussed many times before. The variation of getting the last occurrence of the string is discussed here. Let’s discuss certain ways in which we can find the last occurrence of substring in string in Python.
Using rindex[] to find last occurrence of substring
rindex[] method returns the last occurrence of the substring if present in the string. The drawback of this function is that it throws the exception if there is no substring in the string and hence breaks the code.
Python3
test_string
=
"GfG is best for CS and also best for Learning"
tar_word
=
"best"
print
[
"The original string : "
+
str
[test_string]]
res
=
test_string.rindex[tar_word]
print
[
"Index of last occurrence of substring is : "
+
str
[res]]
Output:
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28
Using rfind[] to find last occurrence of substring
rfind[] is the alternate method to perform this task. The advantage that this function offers better than the above method is that, this function returns a “-1” if a substring is not found rather than throwing the error.
Python3
test_string
=
"GfG is best for CS and also best for Learning"
tar_word
=
"best"
print
[
"The original string : "
+
str
[test_string]]
res
=
test_string.rfind[tar_word]
print
[
"Index of last occurrence of substring is : "
+
str
[res]]
Output:
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28
Using lambda[] with rlocate[] function
Here we are using the more_itertools library that provides us with rlocate[] function that helps us to find the last occurrence of the substring in the given string.
Python3
import
more_itertools as m
test_string
=
"GfG
is
best
for
CS
and
also best
for
Learning"
tar_word
=
"best"
pred
=
lambda
*
x: x
=
=
tuple
[tar_word]
print
[
"The original string : "
+
str
[test_string]]
res
=
next
[m.rlocate[test_string, pred
=
pred,
window_size
=
len
[tar_word]]]
print
[
"Index of last occurrence of substring is : "
+
str
[res]]
Output:
The original string : GfG is best for CS and also best for Learning Index of last occurrence of substring is : 28