How to Compare Two Strings Character by Character in Python [5 different ways]

In this Python tutorial, I’ve explained 5 different methods or techniques of “How to compare two strings character by character in Python.”

I was working on a Python project where I had to validate the string and match all the characters with the user-input string, so these methods and techniques helped me to get the expected result.

Comparing two strings character by character is part of text processing, data validation, and string matching concept which will be useful in realtime projects in Python.

There are 5 different methods to compare two strings character by character in Python.

  1. Using for loop
  2. using while loop
  3. using zip() method
  4. using list comprehension
  5. Using Regex

Compare Two Strings Character by Character in Python using 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.

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

Here is an example to compare two strings character by character in Python.

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 character inside str1 till the length of the 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.

READ:  Python program to find the area of square

Output

Equal
Not Equal
Equal
Equal
Not Equal
Not Equal
Not Equal

Attached are snapshots illustrating the Python code and its corresponding output in Visual Studio.

How to Compare Two Strings Character by Character in Python

Python compares two strings character by character using a while loop with != operator

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

Also, we are using the “!=” operator in Python to compare the strings. It will return True if the strings do not match.

Here is an instance to compare two strings character by character in Python:

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

Here’s a combined view of the Python script and its output within Visual Studio.

How to compare individual characters in two strings in Python

String Comparison by letters in Python using 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. Comparing both the strings using the “is” operator of Python will compare both datatype and character.

READ:  Python List index() method [With Examples]

Output

Not Equal
Not Equal
Equal
Not Equal
Equal

Shown below is a combined screenshot of the Python script and its output in Visual Studio.

Python compare two strings character by character

Python Program to Compare Two Strings by letters using list comprehension

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

Here is an instance to compare two strings character by character in Python using list comprehension:

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

We’ve used the same logic in this program as For loop example, you can refer it through the first example.

Attached is a visual representation of the Python code along with its output within Visual Studio.

compare two strings character by character python

Comparing 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")

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.

Output

Not Equal
Equal
Equal
Not Equal
Equal
Equal
Equal

Shown below is a combined screenshot of the Python script and its output in Visual Studio.

Python Program to Compare Two Strings by letters

Conclusion

Here, we’ve seen how to compare two strings character by character in Python using various methods:

  1. Using for loop
  2. using while loop
  3. using zip() method
  4. using list comprehension
  5. Using Regex

You may also like to read: