'Is there a way to tell if a string slice is in another string in Python?
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.
Solution 1:[1]
The problem in your code is the index number. Use 17 or searchvalue.strip()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Asdoost |
