I am trying to build a small web app in python3 with Flask and I have a problem comparing two strings. For some reason, even if the strings are identical my function returns a false. Show
This is what I am trying to do: I created a (question) form in HTML and I expect an answer from a user. When the user clicks 'Submit' a function is triggered in python that is supposed to compare the answer to the correct answer. If answer == correct_answer then a message is displayed "Excellent!!" if not something like "Try again".
Then I retrieve the answer from the web form and also my correct answer from a .txt file (this is a temporary file that will only contain the correct answer) and call the function: ...
So, the problem is that no matter what the answer is the result is always false. Just to be sure that I have the same values I tried:
and I have the same value, but the function returns false I tried:
The result is 'str' for both print. Any idea why this is not working? In this article, we will learn what is strings in a programming language, how to create them, and their uses. Further, we will study various operators to compare strings in python. At last, we will study some Python strings comparison in brief along with its python code example and output. So, let’s get started! Nội dung chính
Nội dung chính
What are Strings?A string is generally a sequence of characters. A character is a simple symbol. For example, in the English Language, we have 26 characters available. The computer system does not understand characters and hence, therefore, deal with binary numbers. Even though we can see characters on our monitor screens, but internally it is stored and manipulated as a combination of 0s and 1s. The conversion of characters and the binary number is called encoding, and the reverse of this is known as decoding. Some of the popular encodings are ASCII and Unicode. In the Python programming language, a string is a sequence of Unicode characters. Python String Comparison operatorsIn python language, we can compare two strings such as identify whether the two strings are equivalent to each other or not, or even which string is greater or smaller than each other. Let us check some of the string comparison operator used for this purpose below:
String Equals Check in PythonIn python programming we can check whether strings are equal or not using the “==” or by using the “.__eq__” function. Example: s1 = 'String' s2 = 'String' s3 = 'string' # case sensitive equals check if s1 == s2: print('s1 and s2 are equal.') if s1.__eq__(s2): print('s1 and s2 are equal.') Here, we check string s1 and s2 whether they are equal or not, and then use the “if” conditional statement with a combination of the equal operator. The output of the above code is as given below: s1 and s2 are equal. s1 and s2 are equal. What about Case insensitive comparisons?While checking the equality in strings sometimes we wish to ignore the case of the string while comparison. So, as a solution to this, we can use the case fold(), lower(), or upper() function for ignoring the case insensitive comparison of string equality. s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison') The output of the above code is as given below: string string s1 and s3 are equal in case-insensitive comparison string string s1 and s3 are equal in case-insensitive comparison STRING STRING s1 and s3 are equal in case-insensitive comparison ConclusionSo, in this article, we studied how to compare strings in a python programming language. Also, we studied some string comparison operators and check string equality. Even we checked the string case insensitive comparison. Python String comparison can be performed using equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings. Python String ComparisonPython string comparison is performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller. Let’s look through some examples for string comparison.
Output:
Both the strings are exactly the same, hence they are equal. So equality operator is returning True in this case. Let’s look at another example where we will get inputs from the user and then compare them.
Output:
Let’s see if the comparison is case sensitive or not? Also if ‘a’ comes ‘A’?
Output:
So “Apple” is smaller when compared to “apple” because of their Unicode values. We are using ord() function to print the Unicode code point value of the characters. What if one of the string is made of second string and some additional characters?
Output:
Output:
Obviously, both the strings are neither smaller nor greater than the other one. Hence the output is false in both the cases. You can checkout complete python script and more Python examples from our GitHub Repository. Can you use == to compare strings?You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself. How can we compare two strings in Python?Python String comparison can be performed using equality (==) and comparison (<, >, != , <=, >=) operators. There are no special methods to compare two strings. What does == compare in Python?The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None . How do you compare strings in if condition in Python?How to Compare Strings Using the <= Operator. The <= operator checks if one string is less than or equal to another string. Recall that this operator checks for two things – if one string is less or if both strings are the same – and would return True if either is true. We got True because both strings are equal. |