Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Python Program to Count the Number of Matching Characters in a Pair of String
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Python Code to Determine the Amount of Corresponding Characters in Two Strings

    This guide will teach readers how to construct a Python program that compares two strings and returns the number of times those strings contain identical characters.

    The pair of non-empty strings will be sent along. If two strings include identical characters, the computer will tally the number of matches. Now, we’ll pretend that the passed strings contain identical characters. Example:



    1. Input: string_1 =


      ‘Javtpoint’




    2. strint_2 =

      ‘Juvpionk’




    3. Output: no. matching characters in the pairs of strings:

      6




    4. (That is, the matching characters are: – J, v, p, i, o, n)

    5. Input: string_1:

      ‘zyxw531@7#’




    6. string_2:

      ‘xwuv234#’




    7. Output: no. matching characters in the pairs of strings:

      4




    8. (That is, the matching characters are: – x, w,

      3


      , #)

    Approach 1:


    • Step 1:

      we will initialize the counter variable with 0.

    • Step 2:

      we will iterate over the first string from the first character to the last character.

    • Step 3:

      If the character extracted from the string_1 is found in the string_2. And if the first occurrence index of that character in the string_1 is the same as that of the index of the current extracted character, then it will increase the value of the counter by 1.

    Python’s string.find(‘character’) will then be used to locate these similar symbols. If the character is found in the string, this returns its index; otherwise, “-1” is returned. Example:



    1. string_1 =


      ‘zyxwvwwv’




    2. string_1find(

      ‘w’


      ) ?


      3




    3. string_1find(

      ‘v’


      ) ?


      4




    4. string_1find(

      ‘t’


      ) ? –


      1



    Method 1 is a


    • Step 4:

      print the output value of the counter.

    Illustration.



    1. # First, we will define the count function


    2. def count_1(string_1, string_2):

    3. count, find_1 =

      0


      ,


      0





    4. # The loop will execute till the length of string_1 and it will

    5. # Stores the value of string_1 character by character and stores in

      “store_1”


      at every iteration.



    6. for


      store_1 in string_1:



    7. # This will check

      if


      the extracted from of the characters of string_1


    8. # is present in string_2 or not.


    9. if


      string_2.find(store_1) >=


      0


      and find_1 == string_1.find(store_1):


    10. count +=

      1




    11. find_1 +=

      1




    12. print (

      ‘The no. matching characters in the pairs of strings: ‘


      , count)



    13. # Main function

    14. def main():

    15. string_1 = str(input (

      “Please enter the characters for String 1: ”


      ))


    16. string_2 = str(input (

      “Please enter the characters for String 2: ”


      ))


    17. count_1(string_1, string_2) # At last, calling the count function



    18. # Driver Code


    19. if


      __name__ ==


      “__main__”


      :


    20. main()

    Productiveness:

    Please enter the characters for String 1:  ajg 78y
    Please enter the characters for String 2:  gjy 23r
    The no. matching characters in the pairs of strings:  2
    

    Approach 2:


    • Step 1:

      In this method, we will use the set() function for removing the duplicate on the given strings.

    • Step 2:

      We will use the set(intersection) on both strings.

    • Step 3:

      We will use the len() function for calculating the length of the “matched_characters_1” string.

    Second Method Sample

    Resulting



    1. # First, we will define the count function


    2. def count_1(string_1, string_2):

    3. # The set of characters of string_1

    4. set_string_1 = set(string_1)


    5. # The set of characters of string2

    6. set_string_2 = set(string_2)


    7. # We will use

      “&”


      intersection mathematical operation on the sets


    8. # the unique characters present in both the strings

    9. # will be stored in matched_characters_1 set variable

    10. matched_characters_1 = set_string_1 & set_string_2


    11. #Then, we will print the length of matched_characters_1 set

    12. # which will give the number of matched characters in the pair of strings.

    13. Print (

      ‘The number matching characters in the pairs of strings: ‘


      + str (len (matched_characters_1)))




    14. # Driver code


    15. if


      __name__ ==


      “__main__”


      :



    16. string_1 = str(input (

      “Please enter the characters for String 1: ”


      ))


    17. string_2 = str(input (

      “Please enter the characters for String 2: ”


      ))



    18. count_1(string_1, string_2) # At last, calling the count function
    Please enter the characters for String 1:  awe ret #$65
    Please enter the characters for String 2:  rty urw @!34 
    The number matching characters in the pairs of strings: 4
    

    Approach 3:


    • Step 1:

      We will import the Re module.

    • Step 2:

      We will use the re.search() function to check if any character of string_1 is present in string_2 and if it does, then 1 will be added into the counter variable.

    Case Study 3: Approach 3

    Resulting



    1. import


      re


    2. string_1 = str(input (

      “Please enter the characters for String 1: ”


      ))


    3. string_2 = str(input (

      “Please enter the characters for String 2: ”


      ))



    4. count =

      0





    5. for


      store_1 in string_1:



    6. if


      re.search(store_1, string_2):


    7. count = count +

      1




    8. print (

      ‘The number matching characters in the pairs of strings: ‘


      , count)

    :

    Please enter the characters for String 1: learning
    Please enter the characters for String 2: working
    The number matching characters in the pairs of strings: 5
    
    Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language Python Program to Count the Number of Matching Characters in a Pair of String
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleCoroutine in Python
    Next Article Class method vs Static method in Python

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    python

    Introduction to Git and GitHub for Python Developers

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.