Python - How To Compare Two Strings Character by Character in Python

ID : 33

viewed : 122

Tags : PythonPython String

vote vote

96

Python is a general-purpose language that makes working with data of integer type, string type, float type, double type, etc., relatively a seamless task with its easy-to-understand syntax and robust APIs working behind the scenes.

This article will talk about some such task that involves strings. And the task is to compare two strings, character by character, using Python.

Compare Strings Character by Character in Python

In Python, we can compare two strings, character by character, using either a for loop or a while loop.

Since two strings can have different lengths, we must make sure that we only consider the smaller length for iterating over the strings for comparison. For comparison, we would count the number of the same characters in both the strings that lie at the same indexes.

Note, this is just one way to compare two strings.

One can count the frequency of each character present in a string for comparison or compute the hamming distance. Hamming distance is the number of indexes where the characters of the strings are different.

The following Python code implements what we talked about above.

def compare_strings(a, b):     if a is None or b is None:         print("Number of Same Characters: 0")         return          size = min(len(a), len(b)) # Finding the minimum length     count = 0 # A counter to keep track of same characters      for i in range(size):         if a[i] == b[i]:             count += 1 # Updating the counter when characters are same at an index      print("Number of Same Characters:", count)  compare_strings("homophones", "homonyms") compare_strings("apple", "orange") compare_strings("apple", "applepie") compare_strings("pasta", "pizza") compare_strings(None, None) compare_strings(None, "valorant") compare_strings("minecraft", None) 

Output:

Number of Same Characters: 4 Number of Same Characters: 0 Number of Same Characters: 5 Number of Same Characters: 2 Number of Same Characters: 0 Number of Same Characters: 0 Number of Same Characters: 0 

The time complexity of the above code is O(n), and the space complexity is O(1) since we are only storing the count and the minimum length.

The above code uses a for loop. As mentioned above, we can also use a while loop to implement the same functionality. Refer to the following code for the same.

def compare_strings(a, b):     if a is None or b is None:         print("Number of Same Characters: 0")         return          size = min(len(a), len(b)) # Finding the minimum length     count = 0 # A counter to keep track of same characters     i = 0      while i < size:         if a[i] == b[i]:             count += 1 # Updating the counter when characters are same at an index                  i += 1      print("Number of Same Characters:", count)  compare_strings("homophones", "homonyms") compare_strings("apple", "orange") compare_strings("apple", "applepie") compare_strings("pasta", "pizza") compare_strings(None, None) compare_strings(None, "valorant") compare_strings("minecraft", None) 

Output:

Number of Same Characters: 4 Number of Same Characters: 0 Number of Same Characters: 5 Number of Same Characters: 2 Number of Same Characters: 0 Number of Same Characters: 0 Number of Same Characters: 0 

The time complexity of the above code is O(n), and the space complexity is O(1), since we are only storing the count and the minimum length.

  • Related HOW TO?