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! Show
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. 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. In this article, we'll see various operators that can help us check if strings are equal or not. If two strings are equal, the value returned would be In this section, we'll see examples of how we can compare strings using a few operators. But before that, you need to have the following in mind:
How to Compare Strings Using the == OperatorThe
We got a value of Let's make it look a bit more fancy using some conditional logic:
In the code above, we created two strings and stored them in variables. We then compared their values. 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. Both strings in our case were equal, so we had "Both strings are equal" printed. If we changed the first string to "hello", then we would have a different message. Note that using How to Compare Strings Using the != OperatorThe
We're using the same example but with a different operator. The I have commented the code to help you understand better. How to Compare Strings Using the < OperatorThe
This returns We can also use conditional statements here like we did in previous sections. How to Compare Strings Using the <= OperatorThe
Recall that this operator checks for two things – if one string is less or if both strings are the same – and would return We got How to Compare Strings Using the > OperatorThe
Since the string on the left isn't greater than the one on the right, we got How to Compare Strings Using the >= OperatorThe
Since one of both conditions of the operator is true (both strings
are equal), we got a value of ConclusionIn this article, we learned about the various operators you can use when checking the equality of strings in Python with examples. We also saw how case sensitivity can alter the equality of strings. Happy coding! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started Can you use == for strings in Python?Python comparison operators
== : This checks whether two strings are equal. != : This checks if two strings are not equal. < : This checks if the string on its left is smaller than that on its right.
Can I use == to compare two 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 do you check if two strings are equal to each other?The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.
How do you check if two strings are not equal in Python?The != operator checks if two strings are not equal. We're using the same example but with a different operator.
|