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
🌐
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?
Discussions

arrays - Python String Comparison is not working - Stack Overflow
So I have the following chunk of code in python which basically opens a text file, makes an array out of the file's text contents (splitting it line by line) and then proceeds to take input from a ... More on stackoverflow.com
🌐 stackoverflow.com
October 16, 2013
Weird string comparison behavior
I've found out about this here . More on reddit.com
🌐 r/Python
5
3
October 25, 2019
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
python string comparison not working - Stack Overflow
Eventhough the ReleaseNum is populated as 12.04 it is not equalling 12.04 in the string comparison ... You should probably use quotes around it : "12.04", otherwise it will be considered a float object. ... The main issue aside, it feels really odd to be using awk from a Python script like this... ... Since ReleaseNum is a string, when you compare it with 12.04 it returns false, because they're of different types, so when you convert 12.04 to a string by putting quotes, it works... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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…
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-comparison
Python Compare Strings: Methods, Operators & Best Practices | DigitalOcean
Learn how to compare strings in Python using ==, is, and ordering operators, plus casefold() and difflib for tricky cases.
🌐
Replit
replit.com › home › discover › how to compare strings in python
How to compare strings in Python | Replit
April 14, 2026 - The fix is to add a guard clause before the comparison. The condition if name is not None and name.lower() == "admin" first checks if the name variable holds a value. Because Python's and operator uses short-circuit evaluation, it won't attempt ...
🌐
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
Find elsewhere
🌐
Reddit
reddit.com › r/python › weird string comparison behavior
r/Python on Reddit: Weird string comparison behavior
October 25, 2019 -

I was looking for some reading material regarding the following example:

a = 'TEST'
b = 'TEST'

print(a is b) # prints true

c = 'TEST1'
c = c[:4]

print(c)
print(a is c) # prints false

The variable c also contains the string TEST yet it's coming back as false. Now I know this is because the is operator compares the identity rather than the value. However I don't quiet understand why a and b have the same identity. I can recall reading somewhere that this is an optimisation step by Python where it understands that it already created the string TEST so all the next times that it uses this string it points to the same identity rather than filling up the memory with copies.

I was wondering if anyone can point me to some reading material, preferably python docs, where it is explained that two variables that are initialised with the same value end up having the same identity. Thank you very much in advance.

EDIT: Found the answer myself after some more googling. It's called String Interning (https://en.wikipedia.org/wiki/String_interning)

🌐
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.

🌐
freeCodeCamp
freecodecamp.org › news › python-compare-strings-how-to-check-for-string-equality
Python Compare Strings – How to Check for String Equality
March 18, 2022 - If we changed the first string to "hello", then we would have a different message. Note that using = would make the interpreter assume you want to assign one value to another. So make sure you use == for comparison.
🌐
Career Karma
careerkarma.com › blog › python › python compare strings: a step-by-step guide
Python Compare Strings: A Step-By-Step Guide | Career Karma
December 1, 2023 - It’s important to note that string comparisons are case sensitive. So, lowercase letters and uppercase letters will affect the result of the comparisons you perform in your Python program.
🌐
FavTutor
favtutor.com › blogs › compare-strings-python
How to Compare String in Python? (String Comparison 101)
November 9, 2023 - If you need to perform a case-insensitive comparison, convert both strings to the same case (e.g., lowercase or uppercase) before comparing them. Be mindful of the Unicode values of characters when performing lexicographical comparisons. Python compares characters based on their Unicode values, which may result in unexpected outcomes if you're not aware of the underlying character encoding.
🌐
Stack Abuse
stackabuse.com › comparing-strings-using-python
Comparing Strings using Python
June 21, 2023 - Second, we'll go over both the ... A number of examples will help you to understand how to use them. As a basic comparison operator you'll want to use == and !=. They work ......
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-equals
How to Check if Two Strings Are Equal in Python (With Examples) | DigitalOcean
Learn how to compare strings in Python using ==, !=, and other methods. This guide covers edge cases, best practices, and real-world examples.