Find Substring Index in Python
To find the position index of a substring in a string, you can use either the find() method or the index() method.
Utilize the find() method:
s = "Hello, World!"
sub = "World"
index = s.find(sub)
print(index) # 输出 7
Utilize the index() method:
s = "Hello, World!"
sub = "World"
index = s.index(sub)
print(index) # 输出 7
If the sub-string is not present in the original string, the find() method will return -1, whereas the index() method will raise a ValueError exception.