Advertisement
# Signature of find method
find(value)
Parameters | Description |
---|---|
value | This is a string value, which is searched in the invoked string and the method find() returns the first index of value(if found) in the invoked string object, otherwise -1 is returned. |
# Python - Program to find a particular string value in a string.
# Creating a string
s1 = 'Decoding Python'
# Finding the index of a value 'coding' in the string
index1 = s1.find('coding')
print('First Index of code in ', s1 , ' : ' , index1)
# Finding the index of a value 'Py' in the string
index2 = s1.find('Py')
print('First Index of Py in ', s1 , ' : ', index2)
# Finding the index of a value 'thon' in the string
index3 = s1.find('thon')
print('First Index of thon in ', s1 , ' : ', index3)
First Index of code in Decoding Python : 2
First Index of Py in Decoding Python : 9
First Index of thon in Decoding Python : 11
# Python - Program to find a particular string value in a string.
# Creating a string
s1 = 'Humpty Dumpty sat on a wall'
# Finding the index of a value 'haha' in the string
index1 = s1.find('haha')
print('First Index of haha in ', s1 , ' : ', index1)
First Index of haha in Humpty Dumpty sat on a wall : -1
Advertisement
# Python - Program to find a particular value in a String.
s1 = 'Rock and roll!'
s2 = 'Live a happy life!'
index1 = s1.find('ro')
index2 = s2.find('li')
print('First index of ro in ', s1, ' : ', index1)
print('First index of li in ', s1, ' : ', index2)
First index of ro in Rock and roll! : 9
First index of li in Rock and roll! : 13
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement