Python: How to check the number of matching charcters for two string?

I want to ask if there is a better way to check the number of characters matches for two different string. For example when I input the word 'succeed' I want to know how many characters does it match the word 'success' at the exact position. In this case, 'succeed' matches 5 out of 7 characters of 'success'. This function is needed to be implemented into a bigger project. Right now I have a proof of concept python code:

word1 = input('Enter word1>')
word2 = input('Enter word2>')
#word1 is being compared to word2
word1_character_list = list(word1)
word2_character_list = list(word2)
matching_word_count = 0
for index in range(0,len(word2)):
    if word1_character_list[index] == word2_character_list[index]:
        matching_word_count = matching_word_count + 1

I wonder if there is a simpler and/or shorter way to do it? Or a way that use less variables. Any help will be appreciated.

You can iterate through two strings simultaneously using the zip function. Also, its considered to be more pythonic to iterate through items in a list and compare the values rather than indexing the list.

For example:

>>> word1 = 'success'
>>> word2 = 'succeed'
>>> matches = 0
>>> for letter1, letter2 in zip(word1, word2):
    if letter1 == letter2:
        matches +=1

>>> matches

If you wanted to get really creative you could do it in one line:

>>> matches = sum([l1 == l2 for l1, l2 in zip(word1, word2)])

>>> matches


Count the Number of matching characters in a pair of strings , If the character extracted from the first string is found in the second string, then increment the value of the counter by 1. The final answer will be count/2 as the� The match object will have the attributes a, b, and size, where a is the starting index from the string matcher.a, b is the starting index from matcher.b, and size is the length of the match. For example: >>> match Match(a=3, b=0, size=3) >>> matcher.a[match.a:match.a+match.size] 'hon' >>> match.a 3 >>> match.b 0

matching_word_count = sum(map(str.__eq__, word1, word2))

Or more readable:

matching_word_count = sum(c1 == c2 for c1, c2 in zip(word1, word2))

This is because booleans are a subclass of integers in Python, with True == 1 and False == 0.

Count the Number of matching characters in a pair of string in Python, We are given two strings. We need to find the count it of the characters in the first string which are also present in a second string.With setThe� To compare two strings in python, you have to ask from user to enter any two string to check whether the two string are equal or not as shown in the program given below. Python Programming Code to Compare Two Strings Following python program ask from user to enter first and second string to compare the given two string:

You could change your for loop to something like this:

for i, j in zip(word1, word2):
    if i != j:
    matching_word_count += 1

This code starts from the first character and breaks as soon as a character is not the same. If every character needs to be checked, then the code can be easily modified to (as in other answers):

for i, j in zip(word1, word2):
    if i == j:
        matching_word_count += 1

Count letter differences of two strings, Note that I have used an underscore for indicating non-matching characters to Python has the excellent difflib , which should provide the needed functionnality. difference + abs(len(a) - len(b)) #in case the two string are of different lenght,� In Python, strings are sequences of characters, which are effectively stored in memory as an object. Each object can be identified using the id() method, as you can see below. Python tries to re-use objects in memory that have the same value, which also makes comparing objects very fast in Python: $ python Python 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2] on linux2 Type "help

Here is another oneliner using inbuilt zip function :

matches = len([c for c,d in zip(word1,word2) if c==d])

Python compare two strings and check how many chars they have in , Here's a solution using Counter . We make a Counter for each of the words, then find the intersection of those Counter s. That itself is a Counter� Given two strings s1 and s2 consisting of lowercase English alphabets, the task is to count all the pairs of indices (i, j) from the given strings such that s1[i] = s2[j] and all the indices are distinct i.e. if s1[i] pairs with some s2[j] then these two characters will not be paired with any other character.

Python Program to Check Common Letters in Two Input Strings , This is a Python Program to check common letters in the two input strings. Prev - Python Program to Generate a Dictionary that Contains Numbers (between 1� So, to compare and check if the contents of a two strings are equal, we should use == operator i.e. for above string objects == operator will return True i.e. if firstStr == secStr: print('Contents of both Strings are same')

How to find common characters between two strings in python, common_char = s1 & s2 # Letters in both s1 and s2. Output: {'t', 'a' How do you add a number to a string in Python? 1,177 Views View 2 Upvoters. � Answer� Regular expressions provide a more flexible (albeit more complex) way to check strings for pattern matching. Python is shipped with a built-in module for regular expressions, called re. The re module contains a function called search, which we can use to match a substring pattern as follows:

Regular Expression HOWTO — Python 2.7.18 documentation, For example, the regular expression test will match the string test exactly. the size of C's int type that will prevent it from matching over 2 billion a characters;� Find how to code in python on We show you top results so you can stop searching and start finding the answers you need.

  • Similar - 1, 2, 3
  • thank you. But I don't quite understand your second method. If I remember correctly the sum function requires the parameters to be a list. I am new to learning python. Would you mind to elaborate more?
  • Hey Steven, look up "list comprehensions" - I use a list comprehension to create a list of boolean values (True / False) which due to some programming history you can do math on (True == 1, False == 0).
  • maybe operator.eq instead of str.__eq__