Python String Comparison: Strings are the set of characters. In Python, there is no separate Data Type for defining Character.
So, String of length 1 can be used as a Character in Python.
String Comparison can be easily performed with the help of Comparison Operator, Like – ==, !=, <, >, <=, >=.
If you have prior knowledge of C, C++ or Java, then you must have already worked with these Operator in String.
However, if you are new to Programming or you haven’t performed String Comparison Operation yet. Then this tutorial is for you.
As String is one of the important topic to cover in order to learn Python Programming.
In this tutorial, you will also learn about ‘is’ Operator and how it is different from == Operator with the help of an example.
So, let’s start with == and != Operator and then you will learn more Comparison Operator followed by many Python Code Examples.
String Comparison with == and != Operator
You can easily compare two Strings and find out whether the two Strings are equal or not, with the help of Equal to(==) and Not Equal to(!=) Operator in Python.
Let’s see with an Example in which we are taking string value in a country variable.
Then we are comparing the strings with == and != Operator.
country = "Germany" print(country == "Germany") print(country != "Germany")
As in the above code snippet, we can see that the variable country which is holding string “Germany” and the string literal “Germany” are equal.
Therefore, the Equal to(==) operator returns True, whereas the Not Equal to(!=) operator returns False.
A string is compared with another string, by comparing one by one the character of one string with another string.
The character which will have the lower unicode value will be smaller and the one which will have the greater unicode value will be larger.
Let’s see an example in which we are comparing uppercase character and lowercase character.
print("germany" == "Germany") print("germany" > "Germany") print(ord("g")) print(ord("G"))
We know that strings are compared by comparing the character of two strings one by one. So, in the above code snippet, firstly ‘g’ is compared with ‘G’.
Since, the Unicode value of ‘g’ is greater than the Unicode value of ‘G’. Therefore, “germany” is greater than “Germany”.
More String Comparison Operator
Other than == and != operator, there are more comparison operator in Python which are Greater than(>), Less than(<), Greater than or Equal to(>=), Less than or Equal to(<=).
Let’s understand the working of these operators with the help of an example.
name1 = "Davante" name2 = "Dave" print(name1 < name2) print(name1 > name2) print(name1 <= name2) print(name1 >= name2)
In the above code snippet, you can see that we are comparing name1 which is “Davante” with name2 which is “Dave”.
The first character of both the variable is same which is ‘D’. Similarly, the second and third charcter is also same.
But the fourth character of name1 variable is ‘a’, whereas of name2 character is ‘e’.
The Unicode value of ‘a’ is 97 and of ‘e’ is 101. Therefore, “Davante” is smaller than “Dave”.
String Comparison with 'is' Operator
In Python, ‘is’ Operator is used to compare the identity of the two objects, whereas ‘==’ Operator is used to compare the value of two objects.
For ‘is’ Operator, if two variables are pointing to the same object then it will return True, otherwise it will return False.
Let’s understand this with the help of an Example.
list_1 = ['a', 'b', 'c'] list_2 = list_1 list_3 = list(list_1) print(list_1 == list_2) print(list_1 == list_3) print(list_1 is list_2) print(list_1 is list_3) print(id(list_1)) print(id(list_2)) print(id(list_3))
In the above code snippet, you can see that list_1 and list_2 are identical, which means that list_1 and list_2 are the same object. Therefore, list_1 is list_2 returns True.
However, for list_3, new object is created and the values of list_1 are assigned to it. Therefore, list_3 is a different object whereas list_1 and list_2 are same object.
In Python, we can find the address of the objects with the help of id() function. You can see that address of list_1 and list_2 is same and address of list_3 is different.
So, list_1 is list_2 returns True whereas list_1 is list_3 return False.
Using Comparison with User Input Strings
Let’s see another example in which we will take the User Input for String Values and use different Comparison Operator on them.
string1 = input("Enter the value of string1: ") string2 = input("Enter the value of string2: ") string3 = string1 print(string1 == string2) print(string1 != string2) print(string1 <= string2) print(string1 >= string2) print(string1 is string2) print(string1 is string3)
Enter the value of string1: Hello World
Enter the value of string2: hello World
We have learned about different Python String Comparison Operators.
- == (Equal to Operator)
- != (Not Equal to Operator)
- < (Less than Operator)
- > (Greater than Operator)
- <= (Less than or Equal to Operator)
- >= (Greater than or Equal to Operator)
- is (is Operator)
We have also seen a difference between ‘==’ and ‘is’ Operator with Example.