0

Checking if a string contains another, why does the first case turn False?

's161_1189a' in 's161_1189b'

False

's160_1156' in '159:s160_1156'

True

Homap
  • 2,142
  • 5
  • 24
  • 34
  • 2
    because 's161_1189a' in 's161_1189b' are different, one is ending with a and other with b – akash karothiya Aug 05 '16 at 09:26
  • but 's160_1156' and '159:s160_1156' are also different – Homap Aug 05 '16 at 09:26
  • 1
    because in 2nd case, 's160_1156' is subset of '159:s160_1156', thus it will give True – akash karothiya Aug 05 '16 at 09:27
  • 1
    The `in` operator on two strings means: is the left argument text contained in the right argument text, exactly, fully and case sensitive. – Klaus D. Aug 05 '16 at 09:30
  • Some of 's161_1189a' is in 's161_1189b', but *not* all of 's161_1189a' is in 's161_1189b' – doctorlove Aug 05 '16 at 09:31
  • In fact, if you actually want the longest common substring this is a duplicate: http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python, http://stackoverflow.com/questions/18715688/find-common-substring-between-two-strings – doctorlove Aug 05 '16 at 09:32

1 Answers1

1

in operator is used to test if a sequence (list, tuple, string etc.) contains a value. It returns True if the value is present, else it returns False. For example

>>> x = 'subset'
>>>'sub' in x
True
>>>'subsets' in x
False

>>> a = [1, 2, 3, 4, 5]
>>> 5 in a
True
>>> 10 in a
False
akash karothiya
  • 5,736
  • 1
  • 19
  • 29