0

Firstly, I apologise for the stupid question or any errors in my post! (This is my first post!)

Secondly, I have tried to look up the answer to this on Stack Overflow and also the RealPython website (Logical Expressions) but I still can't seem to wrap my head around why this piece of code returns a "True" to the last (uncommented) function call?

I am trying to answer the following question from the Ebook "Introduction to computation and programming using Python" -

"Write a function isIn that accepts two strings as arguments and returns True if either string occurs anywhere in the other, and False otherwise. Hint: you might want to use the built-in str operation in."

If anyone can explain (in layman terms) why the below piece of code is returning "True" to the last function call, I would be very grateful - Again apologies for the stupid question!

def isIn(string1, string2):
    """Input two strings 
       Returns 'True' if either strings are within each other
    """

    if string1 in string2 or string2 not in string1:
        return True
    else:
        return False

# isIn('hello', 'hello')           # Prints True
# isIn('hello', 'hello world')     # Prints True   
isIn('hello world', 'world hello') # Prints True
sali
  • 25
  • 3
  • 1
    It returns `True` because `string2` is not `in string1`. Did you mean `string1 in string2 or string2 in string1`, without the `not`? – tobias_k Feb 04 '21 at 13:14
  • Thanks for your response. I wanted to include the "not in" expression in order to try to answer the question in the ebook but I do not think I am doing it correctly - apologies! – sali Feb 04 '21 at 13:30

4 Answers4

1

Why your functions returns True ?

The if statement if string1 in string2 or string2 not in string1 is made of 3 parts:

  1. string1 in string2
  2. or
  3. string2 not in string1

And you have:

string1 = 'hello world'
string2 = 'world hello'
  • Part 1 (string1 in string2) :

    It evaluates to False, because 'hello world' isn't in 'world hello'

  • Part 3 (string2 not in string1):

    It evaluates to True, because 'world hello' is effectively not present in 'hello world'

  • Part2, or:

    The or will give you:

    • True if at least one of the expression evaluates to True
    • False if all the expression evaluate to False

So you get True

But if you have used and, you would have get False

If sometimes you are in doubt, try some print like these:

# or:
print(True or True) # True
print(True or False) # True
print(False or False) # False

# and:
print(True and True) # True
print(True and False) # False
print(False and False) # false

Answering your comment:

No, 'hello world' isn't in 'world hello' So, what is in 'world hello' ?

  • 'world', 'hello', ' ' (space) and '' (the empty string).
  • And all possible substrings (characters and consecutive characters in the source string, so for example 'h', 'he', 'hel', ' wo', etc.). And note that all items in fact 'world', 'hello', ' ' and '' are substrings to :-)

So, all of this evaluates to true:

# string2 = 'world hello'
'world' in string2
'hello' in string2
' ' in string2
'' in string2
'h' in string2
'e' in string2
'llo' in string2
'llo 'wo' in string2
# etc.

In computer science, a string is a sequence of characters. Each sub-sequence is a substring.

So now, you should have a better understanding of what is a string and what is a substring and you could/should search some informations on the internet if you're interested.

So, what does the in expression ? The in expression, in fact, when working with strings, tells you if the character of the string you're searching in another string, is a substring of this string or not.

To conclude, the sequence of characters 'hello world' is not in the sequence of characters 'world hello'.

Rivers
  • 1,783
  • 1
  • 8
  • 27
  • This is a very thorough explanation - Thank you for breaking it down like that! I wanted to better understand this part of your answer: " Part 1 (string1 in string2) : It evaluates to False, because 'hello world' isn't in 'world hello' " - Forgive me for sounding stupid here but the strings 'hello world' and 'world hello' look the same to me, except they are the other way around, right? I would have thought that the 'in' operator detected this reversal? Apologies again for my lack of understanding! – sali Feb 04 '21 at 13:37
  • You're welcome! I'll add an example at the end of my answer. – Rivers Feb 04 '21 at 13:45
  • Thank you for updating your answer and adding examples! I have modified my code to include a "for" loop and and "if" statement, which appears to work even if the words are switched around... I will most probably have to experiment more with this - Thank you for the help! – sali Feb 04 '21 at 19:38
0

Your If statement is checking 2 condition:

  1. string1 in string2 : which is False because string1 is 'hello world' and it is not present in string2. if string1 would be 'hello' or 'world' or 'world hello' or ' ' it will return True because all these are present in string2.

  2. string2 not in string1 :It will check whether string2 is not present in string1 your function is returning True because of this condition, 'world hello' is not present in string1

NoobCoder
  • 625
  • 1
  • 5
  • 18
0

Adding to the answer of @NoobCoder , your program is returning true because of the second condition:

"or string2 not in string1" which is returning TRUE, as the second string IS NOT in first string.

Then, as you have your conditions: Cond1 OR Cond2, if any of them return TRUE, your final answer will be TRUE.

-1

hey :D I think that you can use a for loop to check if string1 contains words in string2 Idk how to put a python code here since I'm a newbie too :\


but the whole thing is like


for a (a variable) in b (a string, list, etc):


for c in d:

then code the rest

welp...
  • 9
  • 6
  • `for a in d` iterates over the iteratable d. I think it does not really help checking wether a is contained in d. Instead it just runs the loop-body for each element of d. – wuerfelfreak Feb 05 '21 at 06:58