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:
-
Input: string_1 =
‘Javtpoint’
-
strint_2 =
‘Juvpionk’
-
Output: no. matching characters in the pairs of strings:
6
-
(That is, the matching characters are: – J, v, p, i, o, n)
-
Input: string_1:
‘zyxw531@7#’
-
string_2:
‘xwuv234#’
-
Output: no. matching characters in the pairs of strings:
4
-
(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:
-
string_1 =
‘zyxwvwwv’
-
string_1find(
‘w’
) ?
3
-
string_1find(
‘v’
) ?
4
-
string_1find(
‘t’
) ? –
1
Method 1 is a
-
Step 4:
print the output value of the counter.
Illustration.
-
# First, we will define the count function
-
def count_1(string_1, string_2):
-
count, find_1 =
0
,
0
-
-
# The loop will execute till the length of string_1 and it will
-
# Stores the value of string_1 character by character and stores in
“store_1”
at every iteration.
-
for
store_1 in string_1:
-
-
# This will check
if
the extracted from of the characters of string_1
-
# is present in string_2 or not.
-
if
string_2.find(store_1) >=
0
and find_1 == string_1.find(store_1):
-
count +=
1
-
find_1 +=
1
-
print (
‘The no. matching characters in the pairs of strings: ‘
, count)
-
-
# Main function
-
def main():
-
string_1 = str(input (
“Please enter the characters for String 1: ”
))
-
string_2 = str(input (
“Please enter the characters for String 2: ”
))
-
count_1(string_1, string_2) # At last, calling the count function
-
-
-
# Driver Code
-
if
__name__ ==
“__main__”
:
-
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
-
# First, we will define the count function
-
def count_1(string_1, string_2):
-
# The set of characters of string_1
-
set_string_1 = set(string_1)
-
-
# The set of characters of string2
-
set_string_2 = set(string_2)
-
-
# We will use
“&”
intersection mathematical operation on the sets
-
# the unique characters present in both the strings
-
# will be stored in matched_characters_1 set variable
-
matched_characters_1 = set_string_1 & set_string_2
-
-
#Then, we will print the length of matched_characters_1 set
-
# which will give the number of matched characters in the pair of strings.
-
Print (
‘The number matching characters in the pairs of strings: ‘
+ str (len (matched_characters_1)))
-
-
-
# Driver code
-
if
__name__ ==
“__main__”
:
-
-
string_1 = str(input (
“Please enter the characters for String 1: ”
))
-
string_2 = str(input (
“Please enter the characters for String 2: ”
))
-
-
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
-
import
re
-
string_1 = str(input (
“Please enter the characters for String 1: ”
))
-
string_2 = str(input (
“Please enter the characters for String 2: ”
))
-
-
count =
0
-
for
store_1 in string_1:
-
if
re.search(store_1, string_2):
-
count = count +
1
-
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