I'm new to Python. I've gone through other answers.. I can say with some assurance that this may not be a duplicate.
Basically; let us say for example I want to find the occurrence of one of the substrings (stored in a list); and if found? I want it to stop searching for the other substrings of the list!
To illustrate more clearly;
a = ['This', 'containing', 'many']
string1 = "This is a string containing many words"
If you ask yourself, what is the first word in the bigger string string1
that matches with the words in the list a
? The answer will be This
, because the first word in the bigger string string1
that has a match with list of substrings a
is This
a = ['This', 'containing', 'many']
string1 = "kappa pride pogchamp containing this string this many words"
Now, I've changed string1
a bit. If you ask yourself, what is the first word in the bigger string string1
that matches with the words in the list a
? The answer will be containing
, because the word containing
is the first word that appears in the bigger string string1
that also has a match in the list of substrings a
.
and if such a match is found? I want it to stop searching for any more matches!
I tried this:
string1 = "This is a string containing many words"
a = ['This', 'containing', 'many']
if any(x in string1 for x in a):
print(a)
else:
print("Nothing found")
The above code, prints the entire list of substrings. In other words, it checks for the occurrence of ANY and ALL of the substrings in the list a
, and if found; it prints the entire list of substrings.
I've also tried looking up String find() method but I can't seem to understand how to exactly use it in my case
I'm looking for; to word it EXACTLY: The first WORD in the bigger string that matches any of the list of words in the substring and print that word.
or
to find WHICHEVER SUBSTRING (stored in a list of SUBSTRINGS) appears first in a BIGGER STRING and PRINT that particular SUBSTRING.