Advertisement
# Signature of rfind() method
rfind(value)
Parameters | Description |
---|---|
value | This is a string value, which is searched in the invoked string and the method rfind() returns the last index of value(if found), otherwise -1 is returned. |
# Python - Program to find a particular string value in a string.
# Creating a string
s1 = 'Do not give up!'
# Finding the index of a value 'not' in the string
index1 = s1.rfind('not')
print('last index of not in ', s1 , ' : ' , index1)
# Finding the index of a value 'giv' in the string
index2 = s1.rfind('giv')
print('last index of giv in ', s1 , ' : ', index2)
# Finding the index of a value 'o' in the string
index3 = s1.rfind('o')
print('last index of o in ', s1 , ' : ', index3)
last index of not in Do not give up! : 3
last index of giv in Do not give up! : 7
last index of o in Do not give up! : 4
# Python - Program to find a particular string value in a string.
# Creating a string
s1 = 'Hasta la vista!'
# Finding the index of a value 'haha' in the string
index1 = s1.rfind('pasta')
print('last index of pasta in ', s1 , ' : ', index1)
last index of pasta in Hasta la vista! : -1
Advertisement
# Python - Program to find a particular value in a String.
s1 = 'Rock and roll!'
s2 = 'Live a happy life!'
index1 = s1.rfind('ro')
index2 = s2.rfind('li')
print('last index of ro in ', s1, ' : ', index1)
print('last index of li in ', s1, ' : ', index2)
last index of ro in Rock and roll! : 9
last index of li in Rock and roll! : 13
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement