Translate

Saturday, 30 November 2019

Python String Comparison

In this tutorial we are going to see different methods by which we can compare strings in Python. We will also see some tricky cases when the python string comparison can fail and golden rules to get string comparison always right.

Python strings are Immutable. 

This means that once you have created a string then it cannot be modified, if you do modify it then it will create a new python string. Example below will explain the fact.

str1 = 'TheCrazyProgrammer'
str2 = 'TheCrazyProgrammer'

print(id(str1))  # Prints 54154496
print(id(str2))  # Prints 54154496

str1 += '.com'

print(id(str1))  # Prints 54154400

Here when we make change in str1 then the id of the string changes that confirms that a new string object is created. Now one more question remains i.e. why str1 and str2 have the same id ? 

That is because python do memory optimizations and similar string object are stored as only one object in memory. This is also the case with small python integers. 

Now getting to string comparison there are two different methods by which we can compare strings as below.

Python String Comparison

Method 1: Comparing Using is Operator

is and is not operator is used to compare two strings as below:

str1 = 'TheCrazyProgrammer'

str2 = 'TheCrazyProgrammer'

if str1 is str2 :
    print("Strings are equal")  # Prints String are equal 
else :
    print("String are not equal")

The two strings to be compared are written on either side of the is operator and the comparison is made. is operator compares string based on the memory location of the string and not based on the value stored in the string. 

Similarly to check if the two values are not equal the is not operator is used. 

Method 2: Comparing Using == Operator

The == operator is used to compare two strings based on the value stored in the strings. It’s use is similar to is operator.

str1 = 'TheCrazyProgrammer'

str2 = 'TheCrazyProgrammer'

if str1 == str2 :
    print("Strings are equal")  # Prints String are equal 
else :
    print("String are not equal")

Similarly to check if the two strings are not equal the != is used. 

Why the python string being immutable is important?

Even the python strings weren’t immutable then a comparison based on the memory location would not be possible and therefore is and is not operator cannot be used. 

Both of the comparison methods above are case sensitive i.e. ‘Cat’ and ‘cat’ are treated differently. Function below can be used to first convert the string in some particular case and then use them.

  • .lower() : makes the string lowercase 
  • .upper() : makes the string uppercase

So if both strings are first converted into a similar case and then checked then it would make the comparison case insensitive indirectly. Example below will make things more clear. 

str1 = 'TheCrazyProgrammer'

str2 = 'tHecRazyprogrammer'

if str1 == str2 :
    print("Strings are equal")
else :
    print("String are not equal") # Prints String are not equal

if str1.upper() == str2.upper() :
    print("Strings are equal")   # Prints String are equal
else :
    print("String are not equal")

The golden line to remember whenever using the == and is operator is 

== used to compare values and is used to compare identities.

One more thing to remember here is:

if x is y is then x == y is true

It is easily understandable as x and y points to the same memory locations then they must have the same value at that memory location. But the converse is not true. Here is an example to support the same:

a = {"a":1}
c = a.copy()

print(a is c)  # Prints False
print(a == c) # Prints True

In this example c is at a new memory location and that’s why a is c prints false.

The post Python String Comparison appeared first on The Crazy Programmer.



No comments:

Post a Comment