![]() ![]() We also saw how case sensitivity can alter the equality of strings. In this article, we learned about the various operators you can use when checking the equality of strings in Python with examples. Since one of both conditions of the operator is true (both strings are equal), we got a value of True. The >= operator checks if one string is greater than or equal to another string. How to Compare Strings Using the >= Operator Since the string on the left isn't greater than the one on the right, we got False returned to us. The > operator checks if one string is greater than another string. How to Compare Strings Using the Operator I have commented the code to help you understand better. The != is saying the strings are not equal which is False so a message is printed based on those conditions. We're using the same example but with a different operator. Print("Both strings are equal") # return if false It checks for a given condition, if the condition is true, then the set of code present inside the if block will be executed otherwise not. 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. string1 'Hello' string2 'Hello' if string1 string2: print('Both strings are not equal') return if true else: print('Both strings are equal') return if false Both strings are equal We're using the same example but with a different operator. 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. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |