How To Find The Position Of Difference Between Two Strings in Python

To find the position of difference between two Strings in Python, you can use the equal sign or ord() function combined with the if statement. See details below.

Find The Position Of Difference Between Two Strings in Python

Using the equal sign

You can use the equal sign combined with the if statement to find the position of difference between two Strings in Python. In this solution, we iterate one of the two Strings until finding the difference between them and return the desired position. If no position is returned, two Strings are the same.

Look at the example below.

def diff_position(str1=str(), str2=str()):
    position = -1

    # Iterate one of the two Strings.
    for i in range(len(str1)):
        # Use the equal sign to find the position of difference between two Strings.
        if str1[i] != str2[i]:
            position = i
            break

    # Print the result
    if position == -1:
        print('Two Strings are the same!')
    else:
        print('Position: {}'.format(position))

# The Strings to be used.
str1 = 'Hello, I am crvt4722 from LearnShareIT'
str2 = 'Hello! I am crvt4722 from LearnShareIT'
diff_position(str1, str2)

# Try with another case.
str1 = 'Hello, I am crvt4722 from LearnShareIT'
str2 = 'Hello, I am crvt4722 from LearnShareIT'
diff_position(str1, str2)

Output

Position: 5
Two Strings are the same!

Using the ord() function

Besides the equal sign, you can use the ord() function combined with the if statement to find the position of difference between two Strings in Python. Like the previous solution, you also need to iterate one of the two Strings until finding the difference between them and return the desired position. If no position is returned, two Strings are the same.

Look at the example below.

def diff_position(str1=str(), str2=str()):
    position = -1

    # Iterate one of the two Strings.
    for i in range(len(str1)):
        # Use the ord() function to find the position of difference between two Strings.
        if ord(str1[i]) != ord(str2[i]):
            position = i
            break

    # Print the result
    if position == -1:
        print('Two Strings are the same!')
    else:
        print('Position: {}'.format(position))

# The Strings to be used.
str1 = 'Hello, I am crvt4722 from LearnShareIT'
str2 = 'Hello! I am crvt4722 from LearnShareIT'
diff_position(str1, str2)

# Try with another case.
str1 = 'Hello, I am crvt4722 from LearnShareIT'
str2 = 'Hello, I am crvt4722 from LearnShareIT'
diff_position(str1, str2)

Output

Position: 5
Two Strings are the same!

Summary

We have shown you how to find the position of difference between two Strings in Python. The two ways mentioned above quietly have the same uses and functions. Note that you must handle the case that two Strings are the same. Hope you have an enjoyable learning experience. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *