Check if string is substring of another in python
There is two way to solve this issue:
1. Use operator - in.
2. Use string method find.
String for searching substrings:
long_string = "The matrix is a system Neo, that system is our enemy."
If we use the operator 'in' we will get a boolean result: true or false.
print("matrix" in long_string)
print("array" in long_string)
Output:
True
False
If we use the 'find' method we will get a position from what starts substring or -1 if substring not found. We also can set 'start' and 'end' positions for search.
print(long_string.find("matrix"))
print(long_string.find("array"))
print(long_string.find("matrix", 10)) # 10 is start position
Output:
4
-1
-1