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 Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-equals
How to Check if Two Strings Are Equal in Python (With Examples) | DigitalOcean
September 12, 2025 - To fix this, use the == operator for string comparison. ... s1 = 'Hello' s2 = 'Hello' print(s1 is s2) # This will return True if s1 and s2 refer to the same object, not if they have the same value. ... When comparing a string to None or non-string types, Python will raise a TypeError.
Discussions

Strings not evaluating as equal to the string written out
Could it be that the first column is a factor? Try as.character(df$var[1]) == "final answer" and see what it returns More on reddit.com
🌐 r/Rlanguage
5
1
August 31, 2023
Comparing Strings
Hello, I am attempting to teach myself Python using MIT’s OCW. I am trying to program a Hangman game using functions and I need to compare a string (letters_guessed) to the word the computer chose (secret_word (also a string)). I want to do a simple ‘if’ statement, but since I won’t ... More on discuss.python.org
🌐 discuss.python.org
19
0
July 11, 2023
Why doesn't this IF statement work when comparing string?
if answer == “yes” or “Yes”: Is asking if answer is “yes”, or if “Yes” is Truthy if answer == “yes” or answer == “Yes”: Is what you were trying to ask. The reason we do this is because we could be comparing two completely different variables here. One side of ‘or’ has no idea what the other side is doing The most common suggestion to fix this is if answer in [“yes”, “Yes”]: I’d also suggest to ignore case. if answer.lower() in [“yes”, “y”]: More on reddit.com
🌐 r/learnpython
11
7
November 15, 2024
string comparison
You didn't show your code. Python does not compare substrings as the same: "free" == "freedom" # False More on reddit.com
🌐 r/learnpython
6
0
September 10, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-comparison-in-python
String Comparison in Python - GeeksforGeeks
... Explanation: The < and > operators ... comparisons. Strings in Python can be compared case-insensitively by converting both strings to either lowercase or uppercase....
Published   March 18, 2026
🌐
Reddit
reddit.com › r/rlanguage › strings not evaluating as equal to the string written out
r/Rlanguage on Reddit: Strings not evaluating as equal to the string written out
August 31, 2023 -

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.

🌐
Miguendes
miguendes.me › python-compare-strings
How to Compare Two Strings in Python (in 8 Easy Ways)
July 2, 2022 - In this guide, we saw 8 different ways of comparing strings in Python and two most common mistakes. We saw how we can leverage different operations to perform string comparison and how to use external libraries to do string fuzzy matching. ... String comparison is not working?
🌐
Python.org
discuss.python.org › python help
Comparing Strings - Python Help - Discussions on Python.org
July 11, 2023 - Hello, I am attempting to teach myself Python using MIT’s OCW. I am trying to program a Hangman game using functions and I need to compare a string (letters_guessed) to the word the computer chose (secret_word (also a st…
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › why doesn't this if statement work when comparing string?
r/learnpython on Reddit: Why doesn't this IF statement work when comparing string?
November 15, 2024 -

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.

🌐
IONOS
ionos.com › digital guide › websites › web development › how to compare strings in python
How to compare strings in Python - IONOS
November 21, 2023 - The easiest way to compare two Python strings is with Python operators. As with integers or floating-point numbers, com­par­i­son operators can be used to check that strings are equal. However, operators in this context don’t work as they do with numbers, as there are several string prop­er­ties that can be compared.
🌐
CopyProgramming
copyprogramming.com › howto › python-string-comparison-not-working
Python: Ineffectiveness of Python's string comparison operator (==)
April 2, 2023 - Python string comparison (==) not working, PlayerId seems to be a C-String being stored in a unicode String. Background: C uses a nullbyte (\x00) to mark the end of a string. Since this nullbyte is in your string it ends up in the string representation of the object.
🌐
Edureka Community
edureka.co › home › community › categories › python › how to use not equal operator in python
How to use not equal operator in python | Edureka Community
December 21, 2018 - How would you say does not equal? Like if hi == hi: print "hi" elif hi (does not equal ... there something equivalent to == that means "not equal"?
🌐
Educative
educative.io › answers › how-to-compare-two-strings-in-python
How to compare two strings in Python
Note: Some points to remember when using string comparison operators: The comparisons are case-sensitive, hence same letters in different letter cases(upper/lower) will be treated as separate characters · If two characters are different, then their Unicode value is compared; the character with the smaller Unicode value is considered to be lower. ... The code widget below uses the comparison operators that we have talked about above to compare different strings.
🌐
AskPython
askpython.com › home › string equals check in python: using 4 different methods
String Equals Check in Python: Using 4 Different Methods - AskPython
May 24, 2023 - str1 = "Python" str2 = "PYTHON" if(str1.__eq__(str2)): print("str1 is equal to str2") else: print("str1 is not equal to str2") ... As seen in the above example, the result turns out to be FALSE, because the comparison is case-sensitive. So how can we check that strings are equal in a case-insensitive comparison?
🌐
ReqBin
reqbin.com › code › python › l1pjhl0c › python-compare-strings-example
How do I compare strings in Python?
July 27, 2023 - You can also use the 'is' and 'is not' operators to compare strings. Unlike '==' and '!=', the comparison 'is' performed on the string id. To find out the string id, use the id() method. Usually, the is operator 'is' used to compare objects in Python, but you can compare strings in the same way.
🌐
splunktool
splunktool.com › python-string-comparison-not-working
python string comparison (==) not working<!-- --> - splunktool
August 5, 2022 - But it will not always work because there is a fundamental difference in functionality of is and == operator in python.,Python provides various operators to compare strings i.e. +, !=, <, >, <=, >=. When used for comparison these operators return Boolean True or False value.
🌐
You.com
you.com › search › string comparison python
You.com | The search engine you control.
You.com is an ad-free, private search engine that you control. Customize search results with 150 apps alongside web results. Access a zero-trace private mode.
🌐
ItsMyCode
itsmycode.com › python-compare-strings-a-step-by-step-guide
Python Compare Strings: A Step-By-Step Guide - ItsMyCode
October 21, 2024 - They work the same way as with integer and float comparisons. The == (equals) operator returns true if the two string matches with each other otherwise it returns false. The != (not equals) operator returns true if there is no match between two strings otherwise, it returns false.
🌐
Towards Data Science
towardsdatascience.com › home › latest › side-by-side comparison of strings in python
Side-by-side comparison of strings in Python | Towards Data Science
March 5, 2025 - So we have found all matching sequences in the strings. But for comparing texts, the last match "er." only matches a part of a word. I prefer to match only whole words. Since the matcher takes sequences (strings are sequences of characters in Python) we should be adding words to the matcher.
🌐
Faqcode4u
faqcode4u.com › faq › 242709 › python-string-comparison-not-working
Python String Comparison Not Working
def findPlayer2(self, protocol, playerId): cur = self.conn.cursor() cur.execute("SELECT playerId, playerName, rating FROM players WHERE playerId LIKE (%s)", [playerId]) nbResult = cur.rowcount result = cur.fetchone() cur.close() if nbResult > 0: player = Player(protocol, str(result[0]), str(result[1]), result[2]) self.players.append(player) for player in self.players: id1 = str(player.playerId) id2 = str(playerId) print type(id1) print type(id2) print "Comparing '%s' with '%s'" % (id1, id2) # HERE IS THE COMPARISON if id1 == id2: print "Equal! Player found!" return player else: print "Not equal :(" <type 'str'> <type 'str'> Comparing '11111111' with '11111111' Not equal :( import string player = ''.join(filter(lambda c: c in string.printable, player))