Strings in Python are compared with == and != operators. These compare if two Python strings are equivalent or not equivalent, respectively. They return True or False.
In python strings, each of the characters will be compared separately. Characters in the same positions are compared from both strings.
Here at LinuxAPT, we shall look into how Python String Comparison works.
Before performing string comparison:
Relational operators use Unicode to compare characters of strings. The output of the relational operator is a Boolean value.
Following are relational operators that we'll use to compare strings:
The Equal operator checks whether two strings are equal and gives the output as True if they are otherwise the output will be false.
An example to Compare strings using the equal operator is given below:
print("happy" == "happy")
print("happy" == "Happy")
print("happy" == "unhappy")
print("happy" == " happy")
The Output will give:
True
False
False
False
Unicode of 'H' is \u0048 and 'h' is \u0068 so "happy" and "Happy" are not the same. Unicode for space is \u0020 so "happy" and "Happy" are not the same. If Unicode is the same for both strings then we get True, otherwise, we get False.
Not equal operator checks whether two strings are not equal and in this case the output will be true otherwise the output will be false.
An example to Compare strings using not equal operator is given below:
print("happy" != "happy")
print("happy" != "Happy")
print("happy" != "unhappy")
print("happy" != " happy")
The output will give:
False
True
True
True
If Unicode is not the same for both strings then we get True, otherwise False.
Less than operator compares the value of two strings and if the value of the left string is greater than the right string then the output will be true otherwise the output is false.
An example to Compare strings using less than operator is given below:
print("happy" < "happy")
print("happy" < "Happy")
print("happy" < "unhappy")
print("happy" < " happy")
The output will give:
False
False
True
False
Unicode of 'h' is less than 'u' so we get True.
Less than equal to operator checks whether left side string is smaller or equal to the right side string.
An example to Compare strings using less than equal operator is given below:
print("happy" <= "happy")
print("happy" <= "Happy")
print("happy" <= "unhappy")
print("happy" <= " happy")
The output will give:
True
False
True
False
If the condition is true returns True otherwise returns False.
Greater than operator compares the value of two strings and if the value of the left string is greater than the right string then the output will be true otherwise the output is false.
An example to Compare string using greater than operator is given below:
print("happy" > "happy")
print("happy" > "Happy")
print("happy" > "unhappy")
print("happy" > " happy")
The output will give:
False
True
False
True
Greater than equal to operator checks whether the left side string is greater than the right side string.
An example to Compare string using greater than equal to operator is given below:
print("happy" >= "happy")
print("happy" >= "Happy")
print("happy" >= "unhappy")
print("happy" >= " happy")
The output will give:
True
True
False
True
In python 'is' and 'is not' are identity operators.
is operator checks that both strings refer to the same memory object. If they have the same memory reference, it returns True, else it returns false.
An example of is operator is given below:
a= "happy"
b= "Happy"
print(a is a)
print(a is b)
The output will give:
True
False
is not operator checks that both strings refer to the different memory objects. If they have different memory references, it returns True, else it returns false.
An example of is not operator is given below:
a= "happy"
b= "Happy"
c= "unhappy"
d= " happy"
print(a is not a)
print(a is not b)
print(a is not c)
print(a is not d)
The output will give:
False
True
True
True
This article covers how to compare strings in Python. In fact, Python comparison operators can be used to compare strings in Python. These operators are: equal to (==), not equal to (!=), greater than (>), less than (<), less than or equal to (<=), and greater than or equal to (>=).