Home » Python » Python String Comparison Example

About Yatin Batra

Yatin Batra
Yatin has graduated in Electronics & Telecommunication. During his studies, he has been involved with a large number of projects ranging from programming and software engineering to telecommunications analysis. He works as a software developer in the information technology sector where he is mainly involved with projects based on Java and J2EE technologies platform.

Python String Comparison Example

Hello in this tutorial, we will understand the string comparison in python programming.

1. Introduction

Python String Comparison

String in python language is immutable datatypes and compared with == or != operators. They are a set of characters and a string with a single character in python is known as a Character.

In python programming, if different characters are found for comparison then their Unicode values are compared and the character with a lower Unicode value is treated to be smaller. Other operators like <, <=, > and >= can also be used for string comparison and in this tutorial we will see them all in action. Make note that there are no special methods in python programming to compare the strings. Remember you use operators in python programming to perform operations on variables and values.

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python String Comparison Example

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.

2.1 Python String Comparison Example

Let us understand the different string comparison operations with the help of a code snippet.

Python String Comparison Example

# python spring comparison example
# using relation operators like "==", "!=", ">", "<"
# compares the Unicode values of the characters in a string from the zeroth position till the end.
word1 = "JavaCodeGeeks"
word2 = "javacodegeeks"
# false
print("Is JavaCodeGeeks unicode equal to javacodegeeks unicode = {}".format(word1 == word2))
# true
print("Is JavaCodeGeeks unicode not equal to javacodegeeks unicode = {}".format(word1 != word2))
# false
print("Is JavaCodeGeeks unicode greater than javacodegeeks unicode = {}".format(word1 > word2))
# true
print("Is JavaCodeGeeks unicode less than javacodegeeks unicode = {}".format(word1 < word2))
print("\n==========\n")
# using is and is not
# == operator compares the 2 variables based on the actual value
# while "is" keyword compares 2 variables based on their object ids
# != operator compares the 2 variables based on the actual value
# while "is not" keyword compares 2 variables based on their object ids
string1 = "JavaCodeGeeks"
string2 = "javacodegeeks"
string3 = string1
print("Id of string1 = {}".format(hex(id(string1))))
print("Id of string2 = {}".format(hex(id(string2))))
print("Id of string3 = {}".format(hex(id(string3))))
# true
print(string1 is string1)
# false
print(string1 is string2)
# true
print(string1 is string3)
print("\n==========\n")
# user defined function
# using relation operators to compare strings by their Unicode
# <= - less than equal to
# >= = greater than equal to
def compare_str(str1, str2):
    var1 = 0
    var2 = 0
    for i in range(len(str1)):
        if "0" <= str1[i] <= "9":
            var1 += 1
    for i in range(len(str2)):
        if "0" <= str2[i] <= "9":
            var2 += 1
    return var1 == var2
# false
print(compare_str("54", "102"))
# false
print(compare_str("100", "javacodegeeks"))
# true
print(compare_str("1javacodegeeks", "javacodegeeks1"))

If everything goes well the following output will be shown in the IDE console.

Console Output

Is JavaCodeGeeks unicode equal to javacodegeeks unicode = False
Is JavaCodeGeeks unicode not equal to javacodegeeks unicode = True
Is JavaCodeGeeks unicode greater than javacodegeeks unicode = False
Is JavaCodeGeeks unicode less than javacodegeeks unicode = True
==========
Id of string1 = 0x2d79e668130
Id of string2 = 0x2d79e668d70
Id of string3 = 0x2d79e668130
True
False
True
==========
False
False
True

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • String comparison operators in python programming
  • Sample program to understand string comparison operators in python programming

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial about string comparison in python programming.

Download
You can download the full source code of this example here: Python String Comparison Example
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments