Python String Comparison

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.

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")

Output:
True
False

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" &amp;gt; "Germany")
print(ord("g"))
print(ord("G"))

Output:
False
True
103
71

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”.

We can find the Unicode value of any character with the help of ord() function in Python.

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 &lt; name2)
print(name1 &gt; name2)
print(name1 &lt;= name2)
print(name1 &gt;= name2)

Output:
True
False
True
False

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))

Output:
True
True
True
False
2688487871048
2688487871048
2688519425352

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 &lt;= string2)
print(string1 &gt;= string2)
print(string1 is string2)
print(string1 is string3)

Output:
Enter the value of string1: Hello World
Enter the value of string2: hello World
False
True
True
False
False
True

Conclusion

We have learned about different Python String Comparison Operators.

  1. == Equal to Operator
  2. != Not Equal to Operator
  3. < Less than Operator
  4. > Greater than Operator
  5. <= Less than or Equal to Operator
  6. >= Greater than or Equal to Operator
  7. is is Operator

We have also seen a difference between ‘==’ and ‘is’ Operator with Example.

Let me know in the comment section below, if you have any other question or suggestion related to Python String Comparison.

LEAVE A REPLY

Please enter your comment!
Please enter your name here