I think this how to check if a Python string contains another string so this is an interesting question which is asked a lot in the internet.
There are two basic ways of accomplishing this so first of all let’s say we have the string Ronaldo is better than Messi and we want to search so this is string s1.
Now we want to search a certain string in our original string so say we want to search this substring ronaldo in our string s1 then we can simply use the keyword in which is built in Python functionality it’s efficient clean readable.
So this is the best way of doing it in many cases okay if you print it we see that the result is true Ronaldo is a substring in our original string s1 so the result is true.
So if you search for say a substring that does not exist like football in our original string s1 then the result should be false and this is the case okay!
So this is the first method and it’s very intuitive one two okay the second method is to use the function find so we it’s a string method basically so we call the method on our original string s1.
We use the method name find and then we give the substring we want to search so for example une i’ll do now okay so now in this case we don’t have a boolean output what we have index of the search substring.
So here in this case our index is 0 we can also give so say we search for a string that does not exist like football then the output would be minus 1 and what if we search for a string that exists but only in the later part of the original string then our result is the index of this found substring okay.
So it always gives us back the index or minus 1.
If the substring doesn’t exist okay and the Vantage of this method is that we can specify start and stop index for example we can specify start index a1 and stop index 10 which means that we search for the string nasi only within the interval starting with index 1 and ending in index 10 excluded.
So in this case it should not find the string so there is I this minus 1 okay then there’s a third method which uses the regular expression package real so we first have to import 3 this is more advanced method.
It’s it may be the best one if if you look if you are looking for advanced functionality so for example here we can we have the string Renauld is better than messi ronaldo really is better.
If you print now there is a the return value of our of the read dot find oil function called on a certain pattern this is the substring we want to find like ronaldo.
The original string s 1 then we don’t find only one like in the previous examples but we found all occurrences of the string ronaldo in our original string.
So this is a most powerful function we can also use the search method from the repackage so in this case we would call read dot search however it’s not so readable still it’s even more powerful than defined all method because it also gives us.
The index of the found string like the start index and the stop index of the form string but it doesn’t find all strings.
So there are these different advantages and disadvantages of these methods but so the most intuitive method would be to use the in keyword or to use the find function the regular expression function is best.
If you want to build upon more advanced functionality then if you like the last method which is more of a fun way of doing it is to develop your own algorithms or the standard algorithm is to to simply iterate over all indices of the original string as one then try to match all characters of the string to be searched.
If it fails it proceeds with the next index okay so we are going from index 0 try to match the substring then this if you could not match the substring we proceed with index 1 proceed with index – until we reach the final index of the original string.