Python compare strings
Compare two strings means checking if the strings are equal or not equal to each other.
It also includes determining if one string is greater than or less than the other. Python supports several operators for comparing strings.
Checking for equality
Determining whether 2 strings are equal to each other is often required in programming. Python provides a couple of ways to check for string equality.
== operator compares the values of two strings and returns True if they are equal, False otherwise. Example,
# equal strings str1 = "hello" str2 = "hello" print(str1 == str2) # unequal strings str3 = "codippa" print(str1 == str3)
Output
True
False
It is clear from the output that when string values are same, the == operator prints True
and when they are not equal, it prints False
.
Method 2 : Using is operator
is
operator can also be used to check for equality of strings as shown below.
# equal strings str1 = "hello" str2 = "hello" print(str1 is str2) # unequal strings str3 = "codippa" print(str1 is str3)
Output
True
False
is
operator not only compares the value of two strings but also whether they refer to same memory locations.
Thus, if the two strings to be compared have same values and point to same memory locations as in the above example.
One would ask how did the two strings in above example point to the same memory locations.
This is because python uses the concept of interning strings.
This means that if two strings are created with same value, then the second or later string is made to point to same location as the first one(since they have same values), instead of creating a new string.
In that case, is
operator will return True
.
If the two strings to be compared are created in different ways, then is
will not find them equal since they do not have same memory locations despite their values being same as shown below
# initialize a string str1 = "hello codippa" # create a string with same value but differently str2 = "hello" str2 = str2+" codippa" print(str1 is str2)
Output will be False
although the values are same but their memory locations will be obviously different.
Thus, if you only want to check two strings for value, then go with == and do not use is
.
You can also check whether two strings are not equal to each other(opposite of above).
Python provides an inequality operator for this which is composed of an exclamation mark followed by and equal to sign, that is,
!=
It checks the values of strings and returns
True
if the strings are unequal and False
if the strings have same value.Example,
# check for different strings str1 = "hello" str2 = "codippa" print(str1 != str2) # check for same strings str1 = "hello" str2 = "hello" print(str1 != str2)
Python provides another operator to check for inequality which is <>
(less than symbol followed by greater than symbol) but this operator is available from Python version 3.7 onwards.
It works the same way as !=
operator.
Checking String Differences
It is also possible to check if one string is greater than or lesser than the other using less than(>) and greater than(>) operators. These operators return True
or False
as the result.
When two strings are compared using these operators, python compares them lexicographically, that is, it compares the ASCII value of each character of those strings.
Thus, if the first character of the first string is ‘a’ and second string is ‘b’, then the first string will be treated as smaller than second string and < operator will return True
for such comparisons. Example,
str1 = "acd" str2 = "bcd" print(str1 > str2) # will print False print(str1 < str2) # will print True
Two strings compared in the above example differ only in their first character with the ASCII value of first character in first string less than that of second string and the results are obvious.
If you are checking for equality of two strings which have same value but differ in case, then they will be treated as different strings and == operator will return
False
in that case.If you are only concerned with their values, then convert them to same case using
lower()
and upper()
functions and then compare. Example, str1 = "acd" str2 = "ACD" print(str1 == str2) # will print False print(str1.upper() == str2) # will print True print(str1 == str2.lower()) # will print True