Search code examples
regexpython-3.xstring-matchingstring-length

Is there a pythonic way to count the number of leading matching characters in two strings?


For two given strings, is there a pythonic way to count how many consecutive characters of both strings (starting at postion 0 of the strings) are identical?

For example in aaa_Hello and aa_World the "leading matching characters" are aa, having a length of 2. In another and example there are no leading matching characters, which would give a length of 0.


I have written a function to achive this, which uses a for loop and thus seems very unpythonic to me:

def matchlen(string0, string1): # Note: does not work if a string is ''

    for counter in range(min(len(string0), len(string1))):
        # run until there is a mismatch between the characters in the strings
        if string0[counter] != string1[counter]:         
            # in this case the function terminates
            return(counter)

    return(counter+1)

matchlen(string0='aaa_Hello', string1='aa_World') # returns 2
matchlen(string0='another', string1='example') # returns 0

Solution

  • An unexpected function in os.path, commonprefix, can help (because it is not limited to file paths, any strings work). It can also take in more than 2 input strings.

    Return the longest path prefix (taken character-by-character) that is a prefix of all paths in list. If list is empty, return the empty string ('').

    from os.path import commonprefix
    print(len(commonprefix(["aaa_Hello","aa_World"])))
    

    output:

    2