DEV Community


Find substring in string python

oladejo abdullahi
I am web developer and data scientist
・3 min read

Find, index and in are the method use to find the position or looking for substring or character from a string. To use any one of find write the string then dot followed by the substring or character to look for like the formula below
str.find(substring), str.index(substring) and subtring in string.


find is used to find or search a substring from a string. It Return the lowest index in the string where substring is found within the string. if the substring is not found for find method it Return -1.
Let us try to find word in a sentence with the following examples:

sentence="ayomide is in the kitchen" 
print(findIs) #return 8

findMid= sentence.find("mid") 
print(findMid) #return 3

print(findWas) # return -1

findAyo= sentence.find("ayo") #return 1

findIce= sentence.find('ice') #return -1
Enter fullscreen mode Exit fullscreen mode

The code above printed 8,3,-1,1 and -1 respectively which means the first print function which return 8 count each of the character(letter with space) starting from 0 to where it firstly found the word “is”. FindMid return 3 because it can still find it after “a-y-o” while the findAyo return 0 since the word “ayo” is found before no character. Others gives negative for the fact that the word can’t be found.
So with the above examples it is clear that find() search the substring from the string and return its position if found while it return -1 if not found.

Index Method

Another String Method very similar to find is Index function. It is used in the same way as find, the only different in index and find method is that index return error if the substring not found.let’s check out the following code:

sentence="ayomide is in the kitchen"
print(indexIs) #return 8

indexMid= sentence.index("mid")
print(indexMid) #return 3

indexAyo= sentence.index("ayo") #return 1

print(indexWas) # return errror

indexIce= sentence.find('ice') #return error

Enter fullscreen mode Exit fullscreen mode

As you can see each of them do the same things find method did in the first place Except that it return error where find method return -1.

In Method

In method* is different from find and index it only search and return true or false. If the substring is found it return true otherwise it return false. To use In we write the substring followed by in then the string like the formular below.
Subtring in string
Let’s try the following codes:

sentence = 'maxwizard is the programmer'
isThefound= 'the' in sentence #return true

isMaxwizFound='maxwiz' in sentence #return true

print('a' in 'aeiou') #return true
print('o' in 'aeiou') #return true

print('ao' in 'aeiou') #return false
#Note: ao is not there because “o” doesn’t follow “a” anywhere in “aeiou”

Enter fullscreen mode Exit fullscreen mode

Now let’s us do more example of all the three function together.

vowel ='aeiou'
searchE =consonant.find('e') #search the position of  ‘e’ in the consonant 
print(searchE) #  return -1

indexAe=vowel.index('ae') #search the position of ‘ae’ in the vowel
print(indexAe) # return 0

isVowelThere= vowel in alphabet # check if ‘aeiou’ in alphabet
print(isVowelThere) #return false 
#Note isVowel-there return false because “a-e-i-o-u” are not in that order in alphabet

print('love' in 'I love you') #return true
print('0' in '080898') #return true

Enter fullscreen mode Exit fullscreen mode

Let’s try the last examples

print('8' in telNumber)#return 

print(telNumber.find('080'))# return 0
print(telNumber.index('643'))#return 8

print(telNumber.find('44'))#return -1 

sentence = maxwizard is the name of the writer
isTitifound = titi in sentence #retun false
Enter fullscreen mode Exit fullscreen mode

In short find and index are to locate the position of a substring while in is used to confirm if substring is truly in a string or false. Index return error if not found the substring while find return -1. So I recommended find for you. Coding is fun! Enjoy!

Discussion (0)

Forem Open with the Forem app