Introduction to Python Compare Strings
There are no specific data types for either of the variables in Python if it is numeric or string, or character. A single character is also considered as a string, and it can be declared within double or single quotes. In general, the string is defined as a set of characters or characters within single or double quotes. In Python, the string has any operations and functions on it, such as concatenation, slicing, comparison, len(), max(), min(), etc. In this article, we are discussing the comparison of strings Python.
Many operators are used in performing the comparison of strings, such as equality operator (= =), comparison operators like (<, >, <=, >=, !, !=). The simple logic in comparison of strings is that characters are compared to both the strings. It will check the character with a lower Unicode and is considered a smaller value character or larger value character, or equal value character. In this topic, we are going to learn about Python Compare Strings.
How Does String Comparison Work in Python?
In Python, strings use the ASCII value of characters for comparison. Python uses the objects with the same values in memory which makes comparing objects faster. Some basic comparison operator is equal to (= =) and ‘is’ operator. Now let see the example for each of these operators below.
1. The most commonly used comparison operator is equal to (==)
This operator is used when we want to compare two string variables. This can be done using an if statement with equal to (= =) operator.
Syntax:
if string_variable1 = = string_variable2
true
else
false
Example #1
Code:
str1 = 'Educba'
str2 = 'Educba'
if str1 == str2:
print("Strings are same")
else:
print("Strings are different")
Output:
Example #2
Code:
str1 = 'Educba'
str2 = 'Educba training'
if str1 == str2:
print("Strings are same")
else:
print("Strings are different")
The above screenshot is for the above code, which uses equal to (==) operator, which checks the same or different strings.
‘is’ operator is also used for comparison of strings. ‘is’ and equal to (= =) operators are not the same because ‘is’ operator compares two string variables based on the object id it returns true if same string else false if strings are not same.
Syntax:
string_variable1 is string_variable
Example #3
Code:
a = 2
b = 1
c = 2
if a is c:
print "Same"
else:
print "Different"
Output:
2. Not equal to (!=) operator
This operator is used to check the not same string variables. This can be done using equal to operator also as shown above.
Example #1
Code:
str1 = "Educba"
str2 = "Educba"
if str1 != str2:
print"Strings are different"
else:
print"Strings are same"
Output:
Example #2
Code:
str1 = "Educba"
str2 = "Educba training"
if str1 != str2:
print"Strings are different"
else:
print"Strings are same"
In all the above operators, though the strings are the same, then what to do if the order is different to avoid this, we need to first sort the strings and then use them for comparison. This scenario is used when we know that two given string variables are the same, but their order is different, and yet you want the code to print that they are the same strings; to do this, you need to first use the sort function and then compare the given strings.
Syntax:
sorted (string_variable1) = = sorted (string_variable2)
Example #3
Code:
a = "Educab training"
b = "training Educab"
if sorted(a) == sorted(b):
print"Same"
else:
print"Not Same"
Output:
3. Greater than (>) and lesser than (<)
These are also used for comparing two strings. These operators use the Unicode values of both the strings to compare each of them accordingly.
Syntax:
string_variable1 > string_variable2
string_variable1 < string_variable2
Example #1
Code:
str1 = "Educba training"
str2 = "Educba"
if str1 > str2:
print"True"
else:
print"False"
Output:
If the above code has some strings, it will print as false because either string is smaller or greater.
Example #2
Code:
str1 = "Educba"
str2 = "Educba"
if str1 > str2:
print"True"
else:
print"False"
Output:
4. Greater than or equal to ( >=) operator
This operator is used to compare the strings in which the first string is greater or equal to the second string; then, it falsely.
Example #1
Code:
str1 = "Educba training"
str2 = "Educba"
if str1 >= str2:
print"True"
else:
print"False"
Output:
Below is the screenshot for the string 1 is greater than string 2
Example #2
Code:
str1 = "Educba"
str2 = "Educba"
if str1 >= str2:
print"True"
else:
print"False"
Output:
Below is the screenshot for the string 1 is equal to string 2
Example #3
Code:
str1 = "Edu"
str2 = "Educba"
if str1 >= str2:
print"True"
else:
print"False"
Output:
Below is the screenshot for which string1 is smaller than string 2, so it will give false as we are checking for greater than or equal to the string1.
5. less than or equal to (<=)
This also works the same as greater than or equal to, but it will check for the string with lower Unicode among the given strings to compare.
Syntax:
string_variable1 <= string_variable2
Example #1
Code:
str1 = "Edu"
str2 = "Educba"
if str1 <= str2:
print"True"
else:
print"False"
Output:
The below screenshot is for the less than operator to compare whether string1 is less than string 2, then it returns true else false.
Example #2
Code:
str1 = "Edu"
str2 = "Edu"
if str1 <= str2:
print"True"
else:
print"False"
Output:
The below screenshot is for equal to the operator with less than operator; this also yields true if both given strings have the same Unicode values else it will false.
If the string 1 has a higher Unicode value than string 2, it will return false as we are using less than or equal to the operator.
Example #3
Code:
str1 = "Educba"
str2 = "Edu"
if str1 <= str2:
print"True"
else:
print"False"
Output:
Conclusion – Python Compare Strings
In Python, strings are defined as a set of characters where each character has a different Unicode value or ASCII value. There are many different operators which are used to compare the strings, such as equal to (= =) operator, not equal to (!=) operator, greater than (>) operator, less than (<) operator, greater than or equal to operator (>=) and less than or equal to (<=) operator. Another operator is similar to equal to (= =) operator for comparison of strings known as ‘is’ operator, which usually compares the object id of the given strings instead of Unicode values.
Recommended Articles
We hope that this EDUCBA information on “Python Compare Strings” was beneficial to you. You can view EDUCBA’s recommended articles for more information.