Compare Two Strings Character by Character in Python

As a developer, I was working on a project where I had to validate text input from users against a stored reference string.

The tricky part? I needed to check the strings character by character, not just whether they were equal overall.

Over the years, I’ve used multiple approaches to solve this kind of problem in Python. In this tutorial, I’ll show you five simple methods I use to compare two strings character by character.

1: Use the For Loop with the == Operator

We are using a for loop in Python to iterate over the characters of both strings. Using a for loop is a structural way to compare each character of two strings sequentially.

str1 = "Program"
str2 = "PRogRAM"

for i in range(len(str1)):
    if str1[i] == str2[i]:
        print("Equal")
    else:
        print("Not Equal")

In this code, it will iterate over the characters inside str1 till the length of str1. It will compare both strings with the help of the == operator in Python and print Equal or Not Equal based on a given condition.

Output

Equal
Not Equal
Equal
Equal
Not Equal
Not Equal
Not Equal

You can see the output in the screenshot below.

How to Compare Two Strings Character by Character in Python

And “==” is the comparison operator in Python, and it will return True if the strings match.

2: Use a While Loop with != Operator in Python

The while loop in Python provides a simple approach to comparing strings. It offers more flexibility in terms of loop control and termination conditions.

str1 = "Hello"
str2 = "heLLo"
i = 0
while i < len(str1):
    if str1[i] != str2[i]:
        print("Not Equal")
    else:
        print("Equal")
    i+=1

In this example, we compare both strings with the help of the != operator in Python; if the character matches, it returns False.

Output

Not Equal
Equal
Not Equal
Not Equal
Equal

You can see the output in the screenshot below.

How to compare individual characters in two strings

I have also used the “!=” operator in Python to compare the strings. It will return True if the strings do not match.

3. Use zip() method with “is” operator

The zip() method efficiently compares the corresponding characters of two Python strings simultaneously.

Also, we are using the “is” operator, which is used for strong comparison in Python. It will return True if the variable type and values are equal.

Code

str1 = "Hello"
str2 = "hElLo"

for [x,y] in zip(str1,str2):
    if x is y:
        print("Equal")
    else:
        print("Not Equal")

In this code, we are initializing x and y for str1 and str2, and the for loop iterates through the characters of both strings simultaneously with the help of the zip() method.

Output

Not Equal
Not Equal
Equal
Not Equal
Equal

You can see the output in the screenshot below.

Python compare two strings character by character

Comparing both strings using the “is” operator of Python will compare both data types and the characters.

4. Use list comprehension

List Comprehension is preferable because it works faster and takes fewer lines of code than other methods in Python.

str1 = "Program"
str2 = "PRogRAM"

result = [print("Equal") if str1[i] == str2[i] else print("Not Equal") for i in range(len(str1))]

Output

Equal
Not Equal
Equal
Equal
Not Equal
Not Equal
Not Equal

You can see the output in the screenshot below.

compare two strings character by character python

This method uses list comprehension to compare characters of two strings, index by index, and print whether they are equal or not.

Compare Characters of Two Strings in Python using Regex

Regular expressions provide a powerful pattern-matching mechanism for comparing strings. We use the re.match() method, an inbuilt function of the re-module in Python.

Code

import re

str1 = "Program"
str2 = "proGram"

for char1, char2 in zip(str1, str2):
    if re.match(char1, char2):
        print("Equal")
    else:
        print("Not Equal")

Output

Not Equal
Equal
Equal
Not Equal
Equal
Equal
Equal

You can see the output in the screenshot below.

Python Program to Compare Two Strings by letters

The re.match() in Python takes two parameters as 1st character and 2nd character and prints Equal or Not Equal based on the matching characters.

Here, I’ve explained how to compare two strings character by character in Python using various methods, such as using a for loop, using a while loop, using the zip() method, using list comprehension, and using Regex.

    You may also like to read:

    51 Python Programs

    51 PYTHON PROGRAMS PDF FREE

    Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

    pyython developer roadmap

    Aspiring to be a Python developer?

    Download a FREE PDF on how to become a Python developer.

    Let’s be friends

    Be the first to know about sales and special discounts.