Advertisement
# Signature of rindex method
rindex(value)
Parameters | Description |
---|---|
value | This is a string value, which is searched in the invoked string and the method rindex() returns the last index of value(if found), otherwise the error i.e. ValueError is reported. |
# Python - Program to find a particular string value in a string.
# Creating a string
s1 = 'Work hard for your dreams'
# Finding the index of a value 'for' in the string
index1 = s1.rindex('for')
print('last index of for in ', s1 , ' : ' , index1)
# Finding the index of a value 'ard' in the string
index2 = s1.rindex('ard')
print('last index of ard in ', s1 , ' : ', index2)
# Finding the index of a value 'r' in the string
index3 = s1.rindex('r')
print('last index of r in ', s1 , ' : ', index3)
last index of for in Work hard for your dreams : 10
last index of ard in Work hard for your dreams : 6
last index of r in Work hard for your dreams : 20
# Python - Program to find a particular string value in a string.
# Creating a string
s1 = 'Giving up is not a solution'
# Finding the index of a value 'haha' in the string
index1 = s1.rindex('zzz')
print('last index of Py in ', s1 , ' : ', index1)
Traceback (most recent call last):
File "D:/Python Programs/rindex2.py", line 10, in <module>
index1 = s1.rindex('zzz')
ValueError: substring not found
Advertisement
# Python - Program to find a particular value in a String.
# Creating the string
s1 = 'Do what you Love, love what you do'
# Searching for the value 'love' in the string
index1 = s1.rindex('love')
# Searching for the value 'do' in the string
index2 = s1.rindex('do')
print('last index of love in ', s1, ' : ', index1)
print('last index of do in ', s1, ' : ', index2)
last index of love in Do what you Love, love what you do : 18
last index of do in Do what you Love, love what you do : 32
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement