Advertisement
# Signature of index method
index(value)
Parameters | Description |
---|---|
value | This is a string value, which is searched in the invoked string and the method index() returns the first 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 = 'Let us learn Python'
# Finding the index of a value 'us' in the string
index1 = s1.index('us')
print('First Index of us in ', s1 , ' : ' , index1)
# Finding the index of a value 'lea' in the string
index2 = s1.index('lea')
print('First Index of lea in ', s1 , ' : ', index2)
# Finding the index of a value 'Py' in the string
index3 = s1.index('Py')
print('First Index of Py in ', s1 , ' : ', index3)
First Index of us in Let us learn Python : 4
First Index of lea in Let us learn Python : 7
First Index of Py in Let us learn Python : 13
# Python - Program to find a particular string value in a string.
# Creating a string
s1 = 'Let us learn Python'
# Finding the index of a value 'haha' in the string
index1 = s1.index('haha')
print('First Index of Py in ', s1 , ' : ', index1)
Traceback (most recent call last):
File "D:/Python Programs/index.py", line 10, in <module>
index1 = s1.index('haha')
ValueError: substring not found
Advertisement
# Python - Program to find a particular value in a String.
s1 = 'Let us Jazz and jam'
s2 = 'Live and let live'
index1 = s1.index('j')
index2 = s2.index('live')
print('First index of J in ', s1, ' : ', index1)
print('First index of live in ', s1, ' : ', index2)
First index of J in Let us Jazz and jam : 7
First index of live in Let us Jazz and jam : 13
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement