Python Compare Strings - How it works ?

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.


Different methods of comparing strings in python:

  • Comparing Strings using relational operators.
  • Comparing Strings using identity operators.


IMPORTANT

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.


a. Comparing Strings 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 (>=)


1. Equal operator (==)  

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.


2. Not equal operator (!=)

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.


3. Less than operator (<)

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.


4. Less than equal to operator (<=)

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.


5. Greater than operator (>)

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


6. Greater than equal to operator (>=)

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


b. Comparing Strings using identity operators.

In python 'is' and 'is not' are identity operators.

1. is operator

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


2. is not operator

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


[Need help in fixing Python function issues ? We can help you. ]

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 (>=). 

Related Posts