The find(string) method:
This function decides if the string given as an argument is present. If true, it will return the index of the string, if false, it will give a return of -1. Example:
example = 'this is a python tutorial':
search = example.find('python'):
print search
search = example.find('python'):
print search
if you run the above code, it'll print out 10 showing that the string is present at an index of 10. However, replacing the python argument with a non existent string such as 'ball, the function returns -1.
The index(string) method:
This method is very similar to the find() method but the index() method raises an exception instead of -1 if the string is not available and returns the index if it is present. Example:type the codes in the previous method changing 'find' to 'index' and see the result.
Lets see your trick for searching a string in a string. use the comment box below.
Hope this helps!
0 comments:
Post a Comment