Tutorial: String comparison in python with examples

Confused on how to do string comparison in python correctly? We have prepared a definite guide for the same.

Well, we all know that in python or in any other programming language string is a set of characters.

But in python, there is no specific data type for storing a character so we can say that string with length one can be a character in python.

There are so many comparison operators in python by which you can easily perform your string comparison operation.

For example ==, !=, <=,>=, <, >.

The string is one of the most important topics in the programming world.

And if you have some experience in programming maybe you should familiar with these comparison operators also.

And if you are a beginner then this article is also for you.

Now let’s try to understand the comparison operator with an example.

Suggested read How to sort a list in python

How to do String comparison in python?

You can easily compare two strings with the help == and != operator.

Code :

a = “andrew”
print(name == “andrew” )
print(name != “andrew”)

Output :

True
False

Explanation :

In the first line of code, the variable name contains a string “Andrew”.

We compare the variable named a and the actual string with the help of equal(==) operator. Then it will return true because these two strings are similar to each other.

But in another case where we use not equal operator (!=) then it will return false.

Also check :   Best Python Ide for Beginners 2021 Guide - MyTrashCode

These operators compare string by comparing one character of the string with one character of another string.

Let us understand one more concept of string that is a character whose Unicode is larger have larger value and the character which has small Unicode value will be smaller.

Example :

print(“andrew” == “ANDREW”)
print(“andrew”  > “ANDREW”)

Output:

False 
True

Code explanation :

Here we use the equal operator and it compares string one by one character. Here the Unicode value of a is greater than the Unicode value of A.

Suggested read Check Python if string contains another string

The is operator in python

The == operator in python is used to compare the value of two objects, while the is operator is used to comparing the identity of the two objects.

Suppose we have two variable which is pointing towards the same object then the is operator will return true otherwise it will return false.

Let’s try to understand this with an example.

Example code:

list1 = [‘x’, ’y’ ,’z’]
list2 = list1
list3 = list(list1)

print(list1 == list2)
print(list1 == list3)
print(list1 is list2)
print(list1 is list2)
print(id(list1))
print(id(list2))
print(id(list3))

Output :

True
True
True 
False
20101020
20101020
30302020

Explanation of code : 

Now here we can see that list1 and list2 are the same objects.

That’s why the is operator returns the value true while list3 is the different objects made with the help of list1so it returns the value false.

The id() function is very useful in python with the help of this function we can find the address of any objects in python.

Here you can see that the address of list1 is the same as the address of list2.

Also check :   Python for in, while & enumerate loop with list index

But the address of list3 is different from the address of  list1 and the address of list2.

That’s why the list1 is list2 returns true while lis1 is list3 returns false.

Suggested read Python: how to write to file

Other string operators in python

The less than (<), less than equal to(<=) and greater than <, greater than equal to <= operator is used to comparing string in python other than equal to == and not equal to != operator.

Let’s try to understand with an example.

Example code :

a = “Davante”
b = “Dave”

print(a < b)
print(a > b)
print(a <= b)
print(a >= b)
Output : 

    True
    False 
    True 
    False

Explanation of code:

In the above code piece, you can see that we are contrasting a which is “Davante” with b which is “Dave”.

The principal character of both the variable is the same which is ‘D’. Thus, the second and third characters are likewise the same.

But the fourth character of a variable is ‘a’, whereas of b character is ‘e’.

The Unicode value of ‘a’ is 97 and of ‘e’ is 101. Therefore, “Davante” is smaller than “Dave”.

Conclusion :

So we have learned a lot of different operators in python by which you can easily compare two string in python. The string comparison in python is easier than any other programming language out there. stay updated with technology and keep learning new things.

Happy coding.

Get occasional tutorials & guides in your inbox. No spam ever.


Buy Me A Coffee

3 thoughts on “Tutorial: String comparison in python with examples”

  1. Hello, all the time i used to check weblog posts here in the early hours in the daylight, because i love to learn more and more.

    Reply

Leave a Comment