![]() ![]() Print("Both strings are not equal") # return if true The != operator checks if two strings are not equal. How to Compare Strings Using the != Operator Note that using = would make the interpreter assume you want to assign one value to another. If we changed the first string to "hello", then we would have a different message. If these values are the same, we would get one message printed to the console and if they aren't the same, we would have a different message printed.īoth strings in our case were equal, so we had "Both strings are equal" printed. In the code above, we created two strings and stored them in variables. Let's make it look a bit more fancy using some conditional logic: string1 = "Hello" We got a value of True returned because both strings above are equal. Here is an example: print("Hello" = "Hello") The = operator checks if two strings are equal. How to Compare Strings Using the = Operator For example, G has an ASCII value of 71 while g has a value of of 103. Each character in a string has an ASCII value (American Standard Code for Information Interchange) which is what operators look out for, and not the actual character.In this section, we'll see examples of how we can compare strings using a few operators.īut before that, you need to have the following in mind: How to Check for String Equality in Python If two strings are equal, the value returned would be True. In this article, we'll see various operators that can help us check if strings are equal or not. When crafting the logic in your code, you may want to execute different commands depending on the similarities or differences between two or more strings.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |