Skip to content

Latest commit

 

History

History
77 lines (52 loc) · 3.05 KB

Python_Find_differences_between_strings.md

File metadata and controls

77 lines (52 loc) · 3.05 KB



Template request | Bug report | Generate Data Product

Tags: #python #strings #differences #compare #find #string

Author: Benjamin Filly

Description: This notebook will compare two strings and find the differences between them. It is useful for users to quickly identify the differences between two strings.

References:

Input

Setup Variables

  • string1: The first string to compare
  • string2: The second string to compare
string1 = "hello i'm benjamin"
string2 = "hallo, how are you"

Model

Find differences between strings

This function will compare two strings and return the differences between them.

def find_string_differences(string1, string2):
    differences = []

    # Find differences between the strings
    for index, (char1, char2) in enumerate(zip(string1, string2)):
        if char1 != char2:
            differences.append(index)

    # Account for differences in length
    if len(string1) > len(string2):
        for index in range(len(string2), len(string1)):
            differences.append(index)

    elif len(string1) < len(string2):
        for index in range(len(string1), len(string2)):
            differences.append(index)

    return differences
differences = find_string_differences(string1, string2)

# Highlight the differences with ANSI escape sequences
highlighted_string1 = ''
highlighted_string2 = ''

for index in range(max(len(string1), len(string2))):
    if index in differences:
        highlighted_string1 += "\033[93m" + (string1[index] if index < len(string1) else '') + "\033[0m"
        highlighted_string2 += "\033[93m" + (string2[index] if index < len(string2) else '') + "\033[0m"
    else:
        highlighted_string1 += string1[index] if index < len(string1) else ' '
        highlighted_string2 += string2[index] if index < len(string2) else ' '

Output

Display result

# Print the highlighted strings
print(f"String 1: {highlighted_string1}")
print(f"String 2: {highlighted_string2}")