DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on

Find substring in string python

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

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" 
findIs=sentence.find("is") 
print(findIs) #return 8

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

findWas=sentence.find("was")
print(findWas) # return -1

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

findIce= sentence.find('ice') #return -1
print(findIce)
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"
indexIs=sentence.index("is") 
print(indexIs) #return 8

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

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

findWas=sentence.index("was")
print(indexWas) # return errror

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

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
print(isThefound)

isMaxwizFound='maxwiz' in sentence #return true
print(isMaxwizFound)

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.

alphabet="abcdefghijklmnoprstuvwxyz"
consonant="bcdfghjklmnpqrstvwxyz" 
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

telNumber='08035265643'
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
print(isTitiFound)
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!

Top comments (3)

Collapse
 
linehammer profile image
linehammer

Python has no substring methods like python substring() or substr(). Instead, you can use slice syntax to get parts of existing strings. Python slicing is a computationally fast way to methodically access parts of your data. The colons (:) in subscript notation make slice notation - which has the arguments, start, stop and step . It follows this template:

Parameters are enclosed in the square brackets.
Parameters are separated by colon.

string[start: end: step]

start - Starting index of string, Default is 0.
end - End index of string which is not inclusive .
step - An integer number specifying the step of the slicing. Default is 1.

net-informations.com/python/basics...

Collapse
 
maxwizardth profile image
oladejo abdullahi

Thank you very kindly for your observation I really appreciate.

Collapse
 
maxwizardth profile image
oladejo abdullahi

However, if you go through the article I am not trying to explain how to extract sub-string from a string but how to search a specific sub-string from a string. You can try to read the article well to get the message. Thanks for your contribution