Select Page

How to Compare Strings In Python

by | Programming, Python, Tips

This tutorial will go through how to compare strings in Python with the help of code examples.


Python String Refresher

A string is a Python data type. A string is a list of characters in Unicode representation. Python deals with a string as an array of characters. Therefore a string is indexable, and we can access a character or several characters in a string using indexing and slicing.

We can declare strings in Python using single quotes ' ' or double quotes " ". Let’s look at an example of declaring a string and printing the value to the console.

yoda_string = "A Python string this is"

print(yoda_string)
A Python string this is

The program returns A Python string this is to the console.

Python String Comparison With is and is Not Equal To Operators

A relational operator tests or defines some relation between two entities. The list of relational operators in Python are:

OperatorMeaning
Less than
Greater than
≺=Less than or equal to
≻=Greater than or equal to
==Equalty/Equal to
!= or Inequality/Not equal to
Relational Operators

We can use the relational operators to compare the characters that make up strings from the 0th index to the end of the strings. Depending on the operator chosen, the operator will return a Boolean value.

The most commonly used comparison operator is equal to or equality (==). It is helpful for checking if a string is equal to another string. An example of practical use is in form entry, where you are comparing the details a user enters with their saved credentials, like an email address or a phone number.

String comparison is case sensitive. Therefore lowercase and uppercase characters will impact the result of your string comparisons. Let’s look at an example of comparing three strings using the equality operator.

string1 = "Research"

string2 = "Research"

string3 = "research"

if string1 == string2:

    print('string1 and string2 are equal')

else:

    print('string1 and string2 are not equal')

if string2 == string3:
    
    print('string2 and string3 are equal')

else:
    print('string2 and string3 are not equal')

In the above code, we check if the strings string1 and string2 are equal using an if statement. We also compare string2 and string3 using the same logic.

The output of the above code is:

string1 and string2 are equal

string2 and string3 are not equal

In the second case, string2 and string3 are not equal because string2 has a capital first letter and string3 does not. If we want to check the equality of strings and ignore the case of the strings we can use casefold(), lower(), or upper(). Let’s look at case insensitive comparison with the previously declared strings.

if string2.casefold() == string3.casefold():

       print(string2.casefold())

       print(string3.casefold())

       print('string2 and string3 are equal in case-insensitive comparison')

else:

    print(string2.casefold())

    print(string3.casefold())

    print('string2 and string3 are not equal in case-insenstive comparison')

if string2.lower() == string3.lower():

    print(string2.lower())

    print(string3.lower())

    print('string2 and string3 are equal in case-insensitive comparison')

else:

    print(string2.lower())

    print(string3.lower())

    print('string2 and string3 are not equal in case-insenstive comparison')

if string2.upper() == string3.upper():

    print(string2.upper())

    print(string3.upper())

    print('string2 and string3 are equal in case-insensitive comparison')

else:

    print(string2.upper())

    print(string3.upper())

    print('string2 and string3 are not equal in case-insenstive comparison')


The above code gives the following output:

research
research
string2 and string3 are equal in case-insensitive comparison

research
research
string2 and string3 are equal in case-insensitive comparison

RESEARCH
RESEARCH
string2 and string3 are equal in case-insensitive comparison

To learn more about converting strings to uppercase go to the article titled: “How-to Guide for Python Uppercase“.

Python String Comparison Using Other Operators

There are relational operators mentioned in the above table that we can use to compare strings in lexicographic order. Lexicographic order or dictionary refers to the ordering strings by the alphabetical order of their component characters. The operators we can use for lexicographic order comparison are:

  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to

Let’s write a program that checks two names and determines which comes first in the alphabet.

if name_one < name_two:

    print(f'{name_one} comes before {name_two} in the alphabet')

elif name_one > name_two:

    print(f'{name_two} comes before {name_one} in the alphabet')

The code uses an if-elif statement. The if statement checks whether the name Imogen comes before Iris in the alphabet by using the < operator. The elif statement checks whether Iris comes before Imogen in the alphabet. In this case, Imogen comes before Iris in the alphabet. Therefore, the if statement evaluates to True, the elif statement evaluates to False. Let’s run the code to see the outcome:

Imogen comes before Iris in the alphabet

Python String Comparison Using Identity Operator

We can use the is or identity operator to check if the memory reference of two Python objects are the same or not. The Python is operator takes two operands and returns True if both the objects have the same memory reference and False if not. The difference between the equality operator and the identity operator is that it compares two variables based on their actual values. The identity operator compares two variables based on their object IDs. Let’s look at an example using the identity operator to compare two strings.

score_one = "999"

score_two = "999"

if score_one is score_two:

    print("Score 1 is the same as Score 2")

else:

    print("Score 1 and Score 2 are not the same")

If we run the code, we get the following output:

Score 1 is the same as Score 2

We could replace the identity operator with the equality operator in the above code. The identity operator evaluates to True because score_one and score_two have the same object IDs. We can validate this by printing out the object IDs of the two strings using the id keyword.

print(id(score_one))

print(id(score_two))

140401033348144

140401033348144

The object IDs are identical. Generally, you want to use the equality operator when comparing immutable data types like strings and numbers. The identity operator is useful when asserting whether an object is a specified singleton in Python like None, True, or False.

Summary

Congratulations on reading to the end of this tutorial! Comparing strings is a handy feature in Python. For example, you can use string comparison for password matching to access an account. You can use relational operators to compare strings in Python.

If you want case insensitive string comparison, you can use casefold(), upper() or lower() on the strings prior to comparison.

You can also use the identity or is operator to compare strings, which produces the same result as the equality operator ==.

For further reading on strings, go to the article titled “How to Get a Substring From a String in Python“.

For reading on the other built-in string representation, repr(), go to the article: What is repr() in Python?

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!