string.found() supports this feature. string.found(substring) returns the starting index if substring exists, and -1 if not.
st1 = "Hello World!"
st2 = "World"
idx = st1.find(st2)
if idx==-1:
print('not exist')
else:
print('exist at idx', idx)
You can use it with .
You can use the "in" operator if you want to determine only the presence or absence.
st1 = "Hello World!"
st2 = "World"
if st2 in st1:
print('exist')
else:
print('not exist')
© 2024 OneMinuteCode. All rights reserved.