Python

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

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button