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 EqualYou can see the output in the screenshot below.

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+=1In 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
EqualYou can see the output in the screenshot below.

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
EqualYou can see the output in the screenshot below.

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 EqualYou can see the output in the screenshot below.

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
EqualYou can see the output in the screenshot below.

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:
- Use the Python pop() Function
- Use the repeat() Function in Python
- Use the ceil() Function in Python
- Create a Void Function in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.