chevron_left
320 points
13 7 5

In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

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)

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)

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 = 'sharad is the programmer'
	isThefound= 'the' in sentence #return true
	print(isThefound)

	isSharadFound='sharad' in sentence #return true
	print(isSharadFound)

	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”

 

More Posts

HOW TO REMOVE A SUBSTRING FROM STRING IN PYTHON - Oct 23, 2020
HOW TO REMOVE SUBSTRING FROM STRING IN PYTHON jaseera - Oct 23, 2020
a function to find out longest palindrome in a given string Sanjana Sagar - Jun 22
How to find all permutations of a string in Java Hasnain_khan - Oct 13, 2020
How to Remove a Substring from a String in Python hubconcepts - Oct 21, 2020
Python get substring between two characters amna - Apr 13
How to split a string in java Hasnain_khan - Oct 10, 2020
How to find all substrings of a string python? Naymul01 - 3 days
How to find the last occurrence of character in string python ? Naymul01 - 2 days
How to get a substring in C (CPP) raven - Jul 29