Match two strings (char to char) till the first non-match using python

Viewed 827

I am trying to match two strings sequentially till the first the non-matched character and then determine the percentage exact match. My code is like this:

def match(a, b):
    a, b = list(a), list(b)
    count = 0
    for i in range(len(a)):
        if (a[i]!= b[i]): break
        else: count = count + 1
    return count/len(a)

a = '354575368987943'
b = '354535368987000'
c = '354575368987000'
print(match(a,b)) # return 0.267
print(match(a,c)) # return 0.8

Is there any built-in method in python already which can do it faster ? For simplicity assume that both strings are of same length.

3 Answers
Related