Python Program to Count the Number of Matching Characters in a Pair of StringIn this tutorial, we will discuss how the users can write a Python program for counting the number of matching characters in the given pair of strings. We will pass the pair of non-empty strings. The program will count the number of matching characters in that pair of strings. Here, we will consider that the strings we passed are having duplicates of characters in them. Example: Approach 1:
We will then use string.find(‘character’) in Python to find the same characters. This will return the first occurrence index of the character in the string if found; otherwise, it will return “-1”. Example:
Example: Method 1 Output: 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:
Example 2: Method 2 Output: 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:
Example 3: Method 3 Output: 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 ConclusionIn this tutorial, we have discussed different methods of writing a Python program for counting the number of matching characters in the given pair of strings. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263282.html