Scripting

How to Compare Strings in Python

How_to_Compare_Strings_in_Python

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.

  1. By using relational operators
  2. 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.

  1. Equal operator (==)
  2. Not equal operator (!=)
  3. Less than operator (<)
  4. Less than equal to operator (<=)
  5. Greater than operator (>)
  6. 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

Graphical user interface, text Description automatically generated

Figure 1: Compare strings using the equal operator

Output:

Text Description automatically generated with medium confidence

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

Text Description automatically generated

Figure 3: Compare strings using not equal operator

Output:

A picture containing graphical user interface Description automatically generated

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

Text Description automatically generated

Figure 5: Compare strings using less than operator

Output:

Text Description automatically generated with medium confidence

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

Text Description automatically generated

Figure 7: Compare strings using less than equal operator

Output:

Graphical user interface Description automatically generated with medium confidence

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

Text Description automatically generated

Figure 9: Compare string using greater than operator

Output:

Graphical user interface Description automatically generated with medium confidence

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

Text Description automatically generated

Figure 11: Compare string using greater than equal to operator

Output:

Text Description automatically generated with low confidence

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

Text Description automatically generated with medium confidence

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

Text Description automatically generated

Figure 15: is not operator

Output:

A picture containing text Description automatically generated

Figure 16: Output

Similar Posts