Python find helps us to find a search pattern in a given text
Python method find() determines if a sub-string presents in a given string
Syntax:
str.find(str, beg=0 end=len(string))
For Example:
stra = "python find substring"
strb = "substring"
stra.find(strb)
stra.find(strb, 10) #This will search the required sub-string from index 10
stra.find(strb, 20) #This will search the required sub-string from index 20
print stra.find(strb) #12
print stra.find(strb, 10) #12
print stra.find(strb, 16, 30) #-1
print stra.find(strb, 20, 30) #-1
Return Value
find() returns -1 if there is no match, else it will return the index of the match
Example:
Python method find() determines if a sub-string presents in a given string
Syntax:
str.find(str, beg=0 end=len(string))
For Example:
stra = "python find substring"
strb = "substring"
stra.find(strb)
stra.find(strb, 10) #This will search the required sub-string from index 10
stra.find(strb, 20) #This will search the required sub-string from index 20
print stra.find(strb) #12
print stra.find(strb, 10) #12
print stra.find(strb, 16, 30) #-1
print stra.find(strb, 20, 30) #-1
Return Value
find() returns -1 if there is no match, else it will return the index of the match
Example:
def getText():
return "Python find Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices";
input_text = getText()
print input_text.find("Defaults for start")
print "\n"
if (input_text and input_text.find("Defaults for start") != -1):
print "\n Found"
else:
print "\n Not Found"
No comments:
Post a Comment