Compare Strings in Python

How to compare two strings in Python? There are several ways to compare strings to check whether two strings are equal or not, for example, you can compare strings using the ==, != and other operators. Besides operators, the Python string class also provides several methods that do the comparison. Keep in mind that these comparisons are case-sensitive. So, for example, “hello” is not equal to “Hello”.

In this article, we will discuss several ways to compare the given two strings in Python. The string is a set of characters similar to C, C++, and other programming languages.

1. Quick Examples of String Compare

Following are quick examples of how to compare two strings.


# Below are quick examples that compares two strings

# Consider two strings
st1="sparkby examples"
st2="Hello sparkby"

st1 == st2 # 
st1 != st2 #
st1 > st2 #
st1 >= st2 #
st1 <= st2 #
st1.__eq__(st2) #
st1 is st2 #
st1 is not st2 #

st1 = "sparkby"
st2 = "SPARKBY"

# Compare both the strings by converting them into lowercase.
st1.lower() == st2.lower() #

# Compare both the strings by converting them into uppercase.
st1.upper() == st2.upper() #

# Compare both the strings by converting them into equalcase
st1.casefold() == st2.casefold() #

2. String Compare using Relational Operators in Python

The following relational operators can be used to compare two strings in Python.

OperatorDescription
==Equal operator. Used to check if two strings are equal or not. If both are equal, True is returned, Otherwise False is returned.
!=Not equal operator. Used to check if two strings are equal or not. If both are equal, False is returned, Otherwise True is returned.
>Return True, if the first string is greater than the second string (ASCII Comparison), Otherwise False is returned.
>=Return True, if the first string is greater than or equal to the second string (ASCII Comparison), Otherwise False is returned.
<Return True, if the first string is less than the second string (ASCII Comparison), Otherwise False is returned.
<=Return True, if the first string is less than or equal to the second string (ASCII Comparison), Otherwise False is returned.
Python Compare Strings with Relation Operators

2.1 Syntax

Let’s see the syntax for all the above operators.


# Here, st1 is the first string and
# st2 is the second string

# Equal to
st1 == st2

# Notequal to
st1 != st2

# Greater than
st1 > st2

# Greater than or equal to
st1 >= st2

# Less than
st1 < st2

# Less than or equal to
st1 <= st2

2.2 Equal and Not equal operators


# Consider two strings
st1="sparkby examples"
st2="Hello sparkby"

print("First: ",st1)
print("Second: ",st2)

print("Does both strings are equal ?",st1==st2)

print("Both the strings are not equal: ",st1!=st2)
# Output:
# First:  sparkby examples
# Second:  Hello sparkby
# Does both strings are equal ? False
# Both the strings are not equal:  True

As per the output, you can see that

  1. False is returned by == operator because both are not equal.
  2. True is returned by != operator because both are not equal.

2.3 Greater than and Less than operators


st1="sparkby examples"
st2="Hello sparkby"

print("Does st1 greater than st2 ?",st1>st2)
print("Does st1 less than st2 ?",st1 < st2)

# Output:
# Does st1 greater than st2 ? True
# Does st1 less than st2 ? False

As per the output, you can see that

  1. True is returned by > operator because, first character ASCII value (s) in st1 is greater than first character ASCII value (H) in st2.
  2. False is returned by < operator because, first character ASCII value (H) in st2 is less than first character ASCII value (s) in st1.

2.4 Greater than or equal to and Less than or equal to operators


# Consdider two strings
st1="sparkby examples"
st2="to"

print("Does st1 greaterthan or equal to st2 ?",st1 >= st2)
print("Does st1 lessthan or equal to st2 ?",st1 <= st2)

# Output:
# Does st1 greaterthan or equal to st2 ? False
# Does st1 lessthan or equal to st2 ? True

3. String comparison using lower() & upper()

In this scenario, we will compare the Python strings based on the case sensitivity. First, we will convert both the strings to lowercase using lower() or both the strings to uppercase using upper() then we will compare both of them. By this way, we can get rid of case-sensitivity.

3.1 Syntax

Let’s see the syntax for all the above methods.


# Lowercase
st1.lower() == st2.lower()

# Uppercase
st1.upper() == st2.upper()

3.2 Example

Here, we made both the strings to equal case and we are using == operator to compare these two strings.


# Consider two strings
st1="sparkby"
st2="SPARKBY"

print("First: ",st1)
print("Second: ",st2)

# Compare both the strings by converting them into lowercase.
print(st1.lower()==st2.lower())

# Compare both the strings by converting them into uppercase.
print(st1.upper()==st2.upper())

# Output:
# First:  sparkby
# Second:  SPARKBY
# True
# True

4. Compare Strings in Python using casefold()

In this scenario, we will compare the strings by converting both of them to the same case using the casefold() Python function. After that, we will use the == operator to check if they are equal or not.


# Consider two strings
# Consdider two strings
st1="sparkby"
st2="SPARKBY"

print("First: ",st1)
print("Second: ",st2)

# Compare both the strings by converting them into equalcase
print(st1.casefold()==st2.casefold())

# Output:
# First:  sparkby
# Second:  SPARKBY
# True
# True

We made both the strings to equal case and we are using == operator to compare these two strings.

5. String Compare using __eq__()

The __eq__() is an instance method that will return True, if both strings are equal, otherwise False is returned. It is case-sensitive.

5.1 Syntax

Let’s see the syntax.


st1.__eq__(st2)

5.2 Example with __eq__


st1="sparkby"
st2="SPARKBY"

# Compare both the strings
print(st1.__eq__(st2))

# Output:
# False

As per the output, both are not equal.

6. String Compare using Python is & is not Operators

Python is and is not operators are same as == and != operators that compares two strings, But the difference is, is and is not will check and return positive if both strings are having same memory Id’s or address. They have to refer to the same memory.

6.1 Syntax

Let’s see the syntax.


# is operator
st1 is st2

# is not operator
st1 is not st2

6.2 is Operator Example


st1="sparkby"
st2="sparkby"

# Return the Id's
print("st1 Id :",id(st1))
print("st2 Id :",id(st2))

# Compare both the strings
print(st1 is st2)

# Output:
# st1 Id : 140359632565040
# st2 Id : 140359632565040
# True

As per the output, both are equal (Id is same).

6.3 is not Operator Example


st1="sparkby"
st2="sparkby"

# Return the Id's
print("st1 Id :",id(st1))
print("st2 Id :",id(st2))

# Compare both the strings
print(st1 is not st2)

# Output:
# st1 Id : 140359632565040
# st2 Id : 140359632565040
# False

As per the output, both are equal (Id is same).is not will return False if both are the same.

7. Conclusion

In this article, you have learned how to compare two strings in Python using Relational operators, __eq__, is and is not operators. If you want to compare the case sensitivity strings, you have to convert both strings into lower or upper case using lower() or upper() methods. casefold() method can also be used in this case.

Leave a Reply