Warning: session_start(): open(/tmp/sess_f91f61f439828c91253c0760f7a09002, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Find The Position Of Difference Between Two Strings in Python - LearnShareIT

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 *