How to compare strings in Python? (‘is’ vs ‘==’)

In this tutorial, you will learn how to compare strings in Python.

Table Of Contents

A string is a set of characters. Let’s discuss different ways to compare strings.

Compare strings using ‘==’ operator

The == operator returns True, if both the strings are equal, otherwise False is returned.

Syntax:

my_str1 == my_str2

Where my_str1 is the first input string and my_str2 is the second string.

Example:

Here, we will create three different strings and apply the “==” operator to check the equality.

# Consider the first string
my_str1="sireesha1"

print("String 1: ",my_str1)

# Consider the second string
my_str2="sireesha2"

print("String 2: ",my_str2)

# Consider the third string
my_str3="sireesha1"

print("String 3: ",my_str3)

# Check both strings are equal
print("String1 and String2 are equal? ",my_str1 == my_str2)


# Check both strings are equal
print("String1 and String3 are equal? ",my_str1 == my_str3)

Output:

String 1:  sireesha1
String 2:  sireesha2
String 3:  sireesha1
String1 and String2 are equal?  False
String1 and String3 are equal?  True

In the above example, the string “sireesha1” is not equal to “sireesha2” and “sireesha1” is equal to “sireesha1”.

Compare strings using ‘!=’ operator

The != operator in Python returns True, if both the strings are not equal, otherwise False is returned.

Syntax:

my_str1 != my_str2

Where my_str1 is the first input string and my_str2 is the second string.

Example:

Here, we will create three different strings and apply the != operator to check the equality.

# Consider the first string
my_str1="sireesha1"

print("String 1: ",my_str1)

# Consider the second string
my_str2="sireesha2"

print("String 2: ",my_str2)

# Consider  the third string
my_str3="sireesha1"

print("String 3: ",my_str3)

# Check both strings are not equal
print("String1 and String 2 are not equal? ",my_str1!=my_str2)


# Check both strings are not equal
print("String1 and String 3 are not equal? ",my_str1!=my_str3)

Output:

String 1:  sireesha1
String 2:  sireesha2
String 3:  sireesha1
String1 and String 2 are not equal?  True
String1 and String 3 are not equal?  False

The first line, returned True since both the strings are not equal and the second line returned False since both are equal.

Compare strings using ‘>’ operator

The ‘>’ is greater than operator and returns True if the first string is greater than the second string, otherwise False is returned. It compared characters in the strings with Unicode values. In this way, the comparison is performed.

Syntax:

my_str1 > my_str2

Where my_str1 is the first input string and my_str2 is the second string.

Example:

Here, we will create three different strings and apply the > operator to check the equality.

# Consider the first string
my_str1="sireesha1"

print("String 1: ",my_str1)

# Consider the second string
my_str2="sireesha2"

print("String 2: ",my_str2)

# Consider the third string
my_str3="sireesha1"

print("String 3: ",my_str3)

# Check my_str1 is greater than my_str2
print("my_str1 is greater than my_str2 ? ",my_str1 > my_str2)

#check my_str1 is greater than my_str3
print("my_str1 is greater than my_str3 ? ",my_str1 > my_str3)


#check my_str2 is greater than my_str1
print("my_str2 is greater than my_str1 ? ",my_str2 > my_str1)

Output:

String 1:  sireesha1
String 2:  sireesha2
String 3:  sireesha1
my_str1 is greater than my_str2 ?  False
my_str1 is greater than my_str3 ?  False
my_str2 is greater than my_str1 ?  True

Output Analysis:

  • The first line returned False since the first string is not greater than second one.
  • The second line returned False since the first string is not greater than third one.
  • The second line returned True since the second string is not greater than first one.

Compare strings using ‘>=’ operator

The ‘>=’ is greater than or equal to operator and it return True, if the first string is greater than or equal to the second string, otherwise False. It compared characters in the strings with Unicode values. In this way, the comparison is performed.

Syntax:

my_str1 >= my_str2

Where my_str1 is the first input string and my_str2 is the second string.

Example:

Here, we will create three different strings and apply >= operator to check the equality.

# Consider the first string
my_str1="sireesha1"

print("String 1: ",my_str1)

# Consider the second string
my_str2="sireesha2"

print("String 2: ",my_str2)

# Consider the third string
my_str3="sireesha1"

print("String 3: ",my_str3)

# Check mystr1 is greater than or equal to mystr2
print("mystr1 is greater than or equal to mystr2 ? ",my_str1>=my_str2)

# Check mystr1 is greater than or equal to mystr3
print("mystr1 is greater than or equal to mystr3 ? ",my_str1>=my_str3)

Output:

String 1:  sireesha1
String 2:  sireesha2
String 3:  sireesha1
mystr1 is greater than or equal to mystr2 ?  False
mystr1 is greater than or equal to mystr3 ?  True

The first line, returned False since first string is not greater than or equal second string and the second line returned True, since both are equal.

Compare strings using ‘<‘ operator

The ‘<‘ operator is less than operator, and it returns True, if the first string is less than the second string, otherwise False is returned. It compared charcaters in the strings with Unicode values. By this way, comparison is performed.

Syntax:

my_str1 < my_str2

Where, my_str1 is the first input string and my_str2 is the second string.

Example:

Here, we will create three different strings and apply < operator to check the equality.

# consider the first string
my_str1="sireesha1"

print("String 1: ",my_str1)

# Consider the second string
my_str2="sireesha2"

print("String 2: ",my_str2)

# Consider the third string
my_str3="sireesha1"

print("String 3: ",my_str3)

# Check mystr1 is less than mystr2
print("mystr1 is less than mystr2 ? ",my_str1 < my_str2)


# Check mystr1 is less than mystr3
print("mystr1 is less than mystr3 ? ",my_str1 < my_str3)

Output:

String 1:  sireesha1
String 2:  sireesha2
String 3:  sireesha1
mystr1 is less than mystr2 ?  True
mystr1 is less than mystr3 ?  False

The first line returned True since the first string is less than the second string. The second line returned False, the first string is not less than the second string

Compare strings using ‘<=’ operator

The ‘<=’ is greater than or equal to the operator, and it returns True, if the first string is less than or equal to the second string, otherwise False is returned. It compared characters in the strings with Unicode values. In this way, the comparison is performed.

Syntax:

my_str1!<=my_str2

Where my_str1 is the first input string and my_str2 is the second string.

Example:

Here, we will create three different strings and apply the <= operator to check the equality.

# Consider the first string
my_str1="sireesha1"

print("String 1: ",my_str1)

# Consider the second string
my_str2="sireesha2"

print("String 2: ",my_str2)

# Consider the third string
my_str3="sireesha1"

print("String 3: ",my_str3)

# Check mystr1 is less than or equal to mystr2
print("mystr1 is less than or equal to mystr2 ? ",my_str1<=my_str2)

# Check mystr1 is less than or equal to mystr3
print("mystr1 is less than or equal to mystr3 ? ",my_str1<=my_str3)

Output:

String 1:  sireesha1
String 2:  sireesha2
String 3:  sireesha1
mystr1 is less than or equal to mystr2 ?  True
mystr1 is less than or equal to mystr3 ?  True

The first line, returned True since the first string is less than or equal second string and the second line returned True since both are equal.

Compare strings using is operator

is operator is used to comparing two strings. It compares the elements and also compares the memory in which the strings reside. If both the strings and both the memories are the same, it returns True, otherwise False.

Syntax:

mystr1 is my_str2

Where my_str1 is the first input string and my_str2 is the second string.

Example:

Here, we will create three different strings and apply is operator to check the equality.

# Consider the first string
my_str1="sireesha1"

print("String 1: ",my_str1)

# Consider the second string
my_str2="sireesha"
my_str2 = my_str2 + "1"

print("String 2: ",my_str2)

# Consider the third string
my_str3="sireesha1"

print("String 3: ",my_str3)

# Check mystr1 is equal to mystr2
print("mystr1 is  equal to mystr2 ? ", my_str1 is my_str2)


# Check mystr1 is equal to mystr3
print("mystr1 is equal to mystr3 ? ",my_str1 is my_str3)

Output:

String 1:  sireesha1
String 2:  sireesha1
String 3:  sireesha1
mystr1 is  equal to mystr2 ?  False
mystr1 is equal to mystr3 ?  True

The first line, returned False since the first string is not equal to the second string. Here bot the strings had similar characters but memory is different. The second line returned True since both are equal and memory in which the strings reside is same.

Difference between is & ‘==’

The == is used to compare two strings and return True if they are equal but, is operator is used to compare the two strings and also compare the memory of both the strings. If both are matched, it returns True.

Let’s see the example

Example:
In this example, we will create two empty lists a, b, and compare the equality

# create two empty lists
a=[]
b=[]
# Check a is equal to b using ==
print(a==b)
# Check a is equal to b using is
print(a is b)

print(id(a))
print(id(b))

Output:

True
False
139843647801936
139843648145120

We can see that == returned True, since both are equal but is returned False since memory is different. We can return the memory address using the id() function.

Summary

In this Python string tutorial, we explored how to compare two strings using relational operators and saw the differences and functionality of == and is operators.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top