I am working on a larger project but I've arrived at an issue where I am taking a slice from a string and I am attempting to tell if the slice is in a main string. The problem is that even though the types are the same it seems that the 'in' operator and 'index' operators don't work with the slice. Is there a way to tell if a slice is contained in a 'main string' or can you not compare slices and strings in that way? Here is some example code on what I am trying to accomplish:
x = ['dog']
value = 'dog'
y = 'All values with: ' value
searchvalue = y[16:]
for i in x:
if searchvalue in i:
print('Is in')
else:
print('Not in')
So I want to be able to tell that the value 'dog' taken as a slice from 'y' is in 'x'. I fee like it should be simple but I've come to a dead end so far.
CodePudding user response:
You are trying to find dog instead of dog
Change searchvalue to this
searchvalue = y[17:]
CodePudding user response:
You should really consider using regex:
import re
foo = 'some random string'
found = re.match(foo[:3], foo)
It will return None if not found.
CodePudding user response:
The problem in your code is the index number. Use 17 or searchvalue.strip()
