How do i check if two strings are equal in python?

In this article, we will learn what is strings in a programming language, how to create them, and their uses. Further, we will study various operators to compare strings in python. At last, we will study some Python strings comparison in brief along with its python code example and output. So, let’s get started!

What are Strings?

A string is generally a sequence of characters. A character is a simple symbol. For example, in the English Language, we have 26 characters available. The computer system does not understand characters and hence, therefore, deal with binary numbers. Even though we can see characters on our monitor screens, but internally it is stored and manipulated as a combination of 0s and 1s. The conversion of characters and the binary number is called encoding, and the reverse of this is known as decoding.  Some of the popular encodings are ASCII and Unicode. In the Python programming language, a string is a sequence of Unicode characters.

Python String Comparison operators

In python language, we can compare two strings such as identify whether the two strings are equivalent to each other or not, or even which string is greater or smaller than each other. Let us check some of the string comparison operator used for this purpose below:

  • ==: This operator checks whether two strings are equal.
  • !=: This operator checks whether two strings are not equal.
  • <: This operator checks whether the string on the left side is smaller than the string on the right side.
  • <=: This operator checks whether the string on the left side is smaller or equal to the string on the right side.
  • >: This operator checks whether the string on the left side is greater than the string on the right side.
  • >=: This operator checks whether the string on the left side is greater than the string on the right side.

String Equals Check in Python

In python programming we can check whether strings are equal or not using the “==” or by using the “.__eq__” function.

Example:

s1 = 'String'
s2 = 'String'
s3 = 'string'

# case sensitive equals check
if s1 == s2:
    print('s1 and s2 are equal.')

if s1.__eq__(s2):
    print('s1 and s2 are equal.')

Here, we check string s1 and s2 whether they are equal or not, and then use the “if” conditional statement with a combination of the equal operator.

The output of the above code is as given below:

 s1 and s2 are equal.

 s1 and s2 are equal.

What about Case insensitive comparisons?

While checking the equality in strings sometimes we wish to ignore the case of the string while comparison. So, as a solution to this, we can use the case fold(), lower(), or upper() function for ignoring the case insensitive comparison of string equality.

s1 = 'String'
s2 = 'String'
s3 = 'string'

if s1.casefold() == s3.casefold():
    print(s1.casefold())
    print(s3.casefold())
    print('s1 and s3 are equal in case-insensitive comparison')

if s1.lower() == s3.lower():
    print(s1.lower())
    print(s3.lower())
    print('s1 and s3 are equal in case-insensitive comparison')

if s1.upper() == s3.upper():
    print(s1.upper())
    print(s3.upper())
    print('s1 and s3 are equal in case-insensitive comparison')

The output of the above code is as given below:

 string

 string

 s1 and s3 are equal in case-insensitive comparison

 string

 string

 s1 and s3 are equal in case-insensitive comparison

 STRING

 STRING

 s1 and s3 are equal in case-insensitive comparison

Conclusion

So, in this article, we studied how to compare strings in a python programming language. Also, we studied some string comparison operators and check string equality. Even we checked the string case insensitive comparison.

When crafting the logic in your code, you may want to execute different commands depending on the similarities or differences between two or more strings.

In this article, we'll see various operators that can help us check if strings are equal or not. If two strings are equal, the value returned would be True. Otherwise, it'll return False.

In this section, we'll see examples of how we can compare strings using a few operators.

But before that, you need to have the following in mind:

  • Comparisons are case sensitive. G is not the same as g.
  • Each character in a string has an ASCII value (American Standard Code for Information Interchange) which is what operators look out for, and not the actual character. For example, G has an ASCII value of 71 while g has a value of of 103. When compared, g becomes greater than G.

How to Compare Strings Using the == Operator

The == operator checks if two strings are equal. Here is an example:

print("Hello" == "Hello")
# True

We got a value of True returned because both strings above are equal.

Let's make it look a bit more fancy using some conditional logic:

string1 = "Hello"
string2 = "Hello"

if string1 == string2:
    print("Both strings are equal")
else:
    print("Both strings are not equal")
    
# Both strings are equal

In the code above, we created two strings and stored them in variables. We then compared their values. If these values are the same, we would get one message printed to the console and if they aren't the same, we would have a different message printed.

Both strings in our case were equal, so we had "Both strings are equal" printed. If we changed the first string to "hello", then we would have a different message.

Note that using = would make the interpreter assume you want to assign one value to another. So make sure you use == for comparison.

How to Compare Strings Using the != Operator

The != operator checks if two strings are not equal.

string1 = "Hello"
string2 = "Hello"

if string1 != string2:
    print("Both strings are not equal") # return if true
else:
    print("Both strings are equal") # return if false
    
# Both strings are equal

We're using the same example but with a different operator. The != is saying the strings are not equal which is False so a message is printed based on those conditions.

I have commented the code to help you understand better.

How to Compare Strings Using the < Operator

The < operator checks if one string is smaller than the other.

print("Hello" < "hello")

# True

This returns True because even though every other character index in both strings is equal, H has a smaller (ASCII) value than h .

We can also use conditional statements here like we did in previous sections.

How to Compare Strings Using the <= Operator

The <= operator checks if one string is less than or equal to another string.

print("Hello" <= "Hello")

# True

Recall that this operator checks for two things – if one string is less or if both strings are the same – and would return True if either is true.

We got True because both strings are equal.

How to Compare Strings Using the > Operator

The > operator checks if one string is greater than another string.

print("Hello" > "Hello")

# False

Since the string on the left isn't greater than the one on the right, we got False returned to us.

How to Compare Strings Using the >= Operator

The >= operator checks if one string is greater than or equal to another string.

print("Hello" >= "Hello")

# True

Since one of both conditions of the operator is true (both strings are equal), we got a value of True.

Conclusion

In this article, we learned about the various operators you can use when checking the equality of strings in Python with examples. We also saw how case sensitivity can alter the equality of strings.

Happy coding!

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Can you use == for strings in Python?

Python comparison operators == : This checks whether two strings are equal. != : This checks if two strings are not equal. < : This checks if the string on its left is smaller than that on its right.

Can I use == to compare two strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

How do you check if two strings are equal to each other?

The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.

How do you check if two strings are not equal in Python?

The != operator checks if two strings are not equal. We're using the same example but with a different operator.