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!

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R