As it turns out, your string has new-lines (\n\n) at the end.
You can use
text = text.strip()
to remove any surrounding whitespace from your string.
For future cases, to check if your string has the content you think it has:
print(repr(text))
Answer from khelwood on Stack OverflowStrings not evaluating as equal to the string written out
Comparing Strings
Why doesn't this IF statement work when comparing string?
string comparison
Videos
Curious about something I've noticed when working with strings. I have 2 strings that are the same, call it "final answer" and they come from a data frame.
If I try to check equality of the variable from the table with itself written out, it evaluates to false, so df$var[1] == 'final answer' returns false, even though when I print it out it is 'final answer'. if I made df$var[1] == df$var[2] and they are both 'final answer', it returns true.
The weird part for me is it works fine with a different column in the same data frame, where df$var1[1] is "good" and when I would run df$var1 == 'good', it returns true. Not sure why there's a difference, is it the space in the string for the first one? There are also ranges in that first var column, like '<500", which also doesn't work.
I'm new to python and I have this section of code in my guess the number program:
wantplay=input('Do you want to play again? Enter yes or no.\n')
print(playagain)
while True:
if wantplay=='no' or 'No' or 'NO':
playagain=0
break
elif wantplay=='yes' or 'Yes' or 'YES':
break
else:
print('Please enter a valid answer')
print(playagain)
print(wantplay)There is a while loop around most of the program that checks the condition of the variable playagain so if the user doesn't want to play again they can enter some form of no and it should set playagain to zero so the program finishes. If they enter yes then it should not change the value of playagain so the program loops again. I know I still have to clean up the third case where they enter something else, that's not what my question is about.
The problem I'm having is that no matter what I enter as input playagain is set to zero and the program finishes. I added the print statements here so I could see what the playagain and wantplay variables are doing and I get the following output.
Do you want to play again? Enter yes or no.
yes
1
0
yes
And then the program doesn't loop. So playagain is 1 (as expected) before the conditional, it is 0 afterwards, and wantplay contains yes as I expect. Apparently the first block in the conditional is being executed and setting playagain to 0. Why? Am I comparing strings the wrong way or something?
EDIT: Thanks for the help everybody! I understand what I was doing wrong now, lots of good info here. I didn't know lower() or casefold() existed so that answers another question I had about accounting for capitalization in input. I was not sure how you would account for every possibility of caps and lower case and writing out every single one didn't seem like the right way.
when I compare two strings together that contains a substring it returns true. Is there a function or a way to check if they are identical?
free, freedom > False;
I want:
free, freedom = False