As Python developers, we need to do a string comparison from time to time.

You can use different operators to do a string comparison.

Let's look at some of these operators to do a string comparison in python.

Case sensitivity

Before we dive into how to do string comparisons in python.

You should know that any type of string comparison is case sensitive.

The character 'A' is not the same as 'a'.

It's because each character that is part of a string has an ASCII value.

The ASCII value stands for American Standard for Information Interchange.

When you try to do any type of comparison in python using different operators, it will compare the ASCII value of the character.

Actually, no character is compared while doing the comparison, the only thing that is compared is its ASCII value.

'A' has an ASCII value of 65 and 'a' has an ASCII value of 97. When the operator compares 65 and 97, it finds that they are not equal, and therefore returns a certain type of result.

So before we continue, remember that comparisons on strings are case sensitive.

1. String comparison using the == operator

With the help of == operator, you can check if two strings are equal or not.

Let's see this with the help of an example:

print("hey" == "hey")

#Result:
True

The two strings above are both the same.

This is why == returned true instead of false.

str1 = "Hey"
str2 = "HEy"

if(str1 == str2):
    print("Both equal")
else:
    print("Not equal")
# Result:
Not equal

In the example above, I have two strings stored in one variable.

After this I have compared both strings for equality.

Both strings are pronounced the same, but in the python programming language, both strings are not the same.

It is because when comparing two strings their ASCII value is taken into account. The uppercase 'E' has an ASCII value of 69, while the ASCII value of 'e' is 101.

Therefore, two strings stored in the variable str1 and str2 are not equal.

2. String comparison with the != operator

The != is called as the not equals operator.

Checks if two strings are not equal.

It will return true when two strings are not equal and false when two strings are equal.

Let's see this with the help of an example:

str1 = "Hello"
str2 = "Hey"
if(str1 != str2):
    print("Not equal")
else:
    print("Equal")

#Return
Not equal

In the example above, we store two different strings in two separate variables.

We compare both strings.

str1 != str2

The above statement returns a true value.

Then the statement inside the if condition is executed.

Finally, Not Equal is printed.

3. String comparison using <= operator

With the help of the <= operator you can check if one of the strings is less than or equal to another string.

Let's see this with the help of an example:

str1 = "Hey"
str2 = "Hey"
str3 = "CAT"
str4 = "cat"
print(str1 <= str2)
print(str3 <= str4)

#Result:
True
True

In the example above, both str1 and str2 are the same. As a result, the print statement prints True.

With the second print statement, things change a bit.

As we discussed earlier, the comparison operator uses the ASCII value, not the actual characters.

In the string str3, the ASCII value of 'C' equals 67. In the string str4, the ASCII value of 'c' equals 99.

67 is less than 99.

As a result, it prints true.

4. String comparison using >= operator

With the help of the >= operator you can check if a string is greater than or equal to another string.

Let's see this with the help of an example:

str1 = "Hey"
str2 = "Hey"
str3 = "Dog"
str4 = "DoG"
print(str1 >= str2)
print(str3 >= str4)
#Result:
True
True

In the example above, both str1 and str2 are the same. As a result, the print statement prints True.

With the second print statement, things change a bit.

In the string str3, the ASCII value of the first character 'D' equals 68. In the string str4, the ASCII value of the first character 'D' equals 68.

Since the first character of both strings is the same, the ASCII value of the next character is compared. The 'o' character is present in both strings. The ASCII value remains the same.

The third character in str3 is 'g' has an ASCII value of 103.

The third character in str4 is 'G' has an ASCII value of 71.

103 is greater than 71.

As a result, True is printed.

Summary

The ASCII value is used when comparing different strings.

You can use different operators like:

  • ==
  • !=
  • <=
  • >=

Do You Want to Fast Track Your Career as a Programmer

Join a group of people who love programming and technology.

You can join it here.

With the help of our community, we will fix major problems in the life of programmers and discuss frontend as well as backend engineering.

We will help you reprogram your understanding of various things in technology.