In python strings, each of the characters will be compared separately. Characters in the same positions are compared from both strings. If the given comparison condition is fulfilled, then it checks the next characters in the string. Otherwise, it returns False. There are two ways to compare strings in python.
- By using relational operators
- By using identity operators
Some points to remember before performing string comparison:
- Upper and lower case letters are treated as separate characters.
- The unicode value of characters is compared. The character with the greater Unicode value is considered to be higher.
1. By using relational operators
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.
- Equal operator (==)
- Not equal operator (!=)
- Less than operator (<)
- Less than equal to operator (<=)
- Greater than operator (>)
- Greater than equal to operator (>=)
Equal operator (==)
It checks whether two strings are equal and gives the output as True if they are otherwise the output will be false.
print("happy" == "happy") print("happy" == "Happy") print("happy" == "unhappy") print("happy" == " happy")
Output:
True
False
False
False
Figure 1: Compare strings using the equal operator
Output:
Figure 2: Output
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 as shown in figure 2.
Not equal operator (!=)
It checks whether two strings are not equal and in this case the output will be true otherwise the output will be false.
print("happy" != "happy") print("happy" != "Happy") print("happy" != "unhappy") print("happy" != " happy")
Output:
False
True
True
True
Figure 3: Compare strings using not equal operator
Output:
Figure 4: Output
If Unicode is not the same for both strings then we get True, otherwise False as shown in figure 4.
Less than operator (<)
It 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
print("happy" < "happy") print("happy" < "Happy") print("happy" < "unhappy") print("happy" < " happy")
Output:
False
False
True
False
Figure 5: Compare strings using less than operator
Output:
Figure 6: Output
Unicode of ‘h’ is less than ‘u’ so we get True as shown in figure 6.
Less than equal to operator (<=)
It checks whether left side string is smaller or equal to the right side string.
print("happy" <= "happy") print("happy" <= "Happy") print("happy" <= "unhappy") print("happy" <= " happy")
Output:
True
False
True
False
Figure 7: Compare strings using less than equal operator
Output:
Figure 8: Output
If the condition is true returns True otherwise returns False as shown in figure 8.
Greater than operator (>)
It 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
print("happy" > "happy") print("happy" > "Happy") print("happy" > "unhappy") print("happy" > " happy")
Output:
False
True
False
True
Figure 9: Compare string using greater than operator
Output:
Figure 10: Output
Greater than equal to operator (>=)
It checks whether the left side string is greater than the right side string.
print("happy" >= "happy") print("happy" >= "Happy") print("happy" >= "unhappy") print("happy" >= " happy")
Output:
True
True
False
True
Figure 11: Compare string using greater than equal to operator
Output:
Figure 12: Output
2. By using identity operators
In python is and is not are identity operators.
is operator
It checks that both strings refer to the same memory object. If they have the same memory reference, it returns True, else it returns false.
a= "happy" b= "Happy" print(a is a) print(a is b)
Output:
True
False
Figure 13: is operator
Output:
Figure 14: Output
is not operator
It checks that both strings refer to the different memory objects. If they have different memory references, it returns True, else it returns false.
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)
Output:
False
True
True
True
Figure 15: is not operator
Output:
Figure 16: Output