Home > OS >  Python IF True/False
Python IF True/False

Time:09-17

When "Python" is detected in the text (or not) it outputs either True or False. Now i want to take those and use them to print different statements like "is there" or "is not" but i doesn't work.

Text = "Python for beginners"
print("Python" in Text)

if Text == True:
    print("Its there!")
else:
    print("its not there")

The problem is probably with the if Text == True: statement but I just cant get it right. I already tried if Text is True: and if Text == "True":however nothing worked. So if someone here could help me out id be really happy

CodePudding user response:

You have the right check in your print statement "Python" in text, you just don't use it in the right way. You can either store the value in a variable:

is_found = "Python" in text

if is_found:
    print("Its there!")
else:
    print("its not there")

or use it directly in the conditional:

if "Python" in text:
    print("Its there!")
else:
    print("its not there")

but by just printing the value, you are unable to access the value later in your conditional without repeating the same check.

CodePudding user response:

You are only printing the check you made earlier, you would want something like this:

Text = "Python for beginners"

if "Python" in Text:
    print("Its there!")
else:
    print("its not there")

What happens here is python will run

"Python" in Text

as an expression that will return True, and so it will go into the if statement, and print the Its there!.

CodePudding user response:

Text = "Python for beginners"

if "Python" in Text:
    print("Its there!")
else:
    print("Its not there")

Now It will print .

  • Related